I need the current Datetime minus myDate1
in seconds.
DateTime myDate1 = new DateTime(1970, 1, 9, 0, 0, 00); DateTime myDate2 = DateTime.Now; TimeSpan myDateResult = new TimeSpan(); myDateResult = myDate2 - myDate1;
.
.
I tried different ways to calculate but to no effect.
TimeSpan mySpan = new TimeSpan(myDate2.Day, myDate2.Hour, myDate2.Minute, myDate2.Second);
.
The way it's calculated doesn't matter, the output should just be the difference these to values in seconds.
To convert a DateTime to a TimeSpan you should choose a base date/time - e.g. midnight of January 1st, 2000, and subtract it from your DateTime value (and add it when you want to convert back to DateTime ). If you simply want to convert a DateTime to a number you can use the Ticks property.
A TimeSpan value represents a time interval and can be expressed as a particular number of days, hours, minutes, seconds, and milliseconds.
Code:
TimeSpan myDateResult = DateTime.Now.TimeOfDay;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With