Convert Difference between 2 times into Milliseconds?

后端 未结 11 2192
梦毁少年i
梦毁少年i 2020-12-06 09:11

I have two masked TextBox controls and was wondering how I\'d go about getting the time in each one and then converting the difference into milliseconds. Like, say in tb1 I

11条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 09:25

    Many of the above mentioned solutions might suite different people.
    I would like to suggest a slightly modified code than most accepted solution by "MusiGenesis".

    DateTime firstTime = DateTime.Parse( TextBox1.Text );
    DateTime secondTime = DateTime.Parse( TextBox2.Text );
    double milDiff = secondTime.Subtract(firstTime).TotalMilliseconds;
    

    Considerations:
    - earlierTime.Subtract(laterTime) you will get a negative value.
    - use int milDiff = (int)DateTime.Now.Subtract(StartTime).TotalMilliseconds; if you need integer value instead of double
    - Same code can be used to get difference between two Date values and you may get .TotalDays or .TotalHours insteaf of .TotalMilliseconds

提交回复
热议问题