Datetime.now as TimeSpan value?
Asked Answered
B

7

28

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.

Beginner answered 13/8, 2012 at 7:4 Comment(3)
You don't need to do myDateResult = new TimeSpan(); - you don't have to have an initializing expression for every variable and/or you could make the initializer the following expression.Brott
@user1559441, you have already calculated the difference in TimeSpan myDateResult, you may use TotalSeconds property to get the difference in secondsLindstrom
yeah, .TotalSeconds was the answer. thanksBeginner
G
31

Your code is correct. You have the time difference as a TimeSpan value, so you only need to use the TotalSeconds property to get it as seconds:

DateTime myDate1 = new DateTime(1970, 1, 9, 0, 0, 00);
DateTime myDate2 = DateTime.Now;

TimeSpan myDateResult;

myDateResult = myDate2 - myDate1;

double seconds = myDateResult.TotalSeconds;
Gilmagilman answered 13/8, 2012 at 7:18 Comment(0)
N
48

Code:

TimeSpan myDateResult = DateTime.Now.TimeOfDay;
Negate answered 25/3, 2015 at 9:23 Comment(2)
please use code markup (add 4 spaces before the code line), i'd edit it myself but it's such a tiny change that the system won't let me.Drummer
This should be the accepted answer, as it matches 100% the topic question.Sauterne
G
31

Your code is correct. You have the time difference as a TimeSpan value, so you only need to use the TotalSeconds property to get it as seconds:

DateTime myDate1 = new DateTime(1970, 1, 9, 0, 0, 00);
DateTime myDate2 = DateTime.Now;

TimeSpan myDateResult;

myDateResult = myDate2 - myDate1;

double seconds = myDateResult.TotalSeconds;
Gilmagilman answered 13/8, 2012 at 7:18 Comment(0)
A
7

Have you tried something like

DateTime.Now.Subtract(new DateTime(1970, 1, 9, 0, 0, 00)).TotalSeconds

DateTime.Subtract Method (DateTime)

TimeSpan.TotalSeconds Property

Allegedly answered 13/8, 2012 at 7:6 Comment(0)
G
5

How about

myDateResult.TotalSeconds

http://msdn.microsoft.com/en-us/library/system.timespan.totalseconds

Garret answered 13/8, 2012 at 7:6 Comment(0)
N
2

you need to get .TotalSeconds property of your timespan :

DateTime myDate1 = new DateTime(2012, 8, 13, 0, 05, 00);
DateTime myDate2 = DateTime.Now;
TimeSpan myDateResult = new TimeSpan();
myDateResult = myDate2 - myDate1;
MessageBox.Show(myDateResult.TotalSeconds.ToString());
Nolasco answered 13/8, 2012 at 7:12 Comment(0)
R
1

You can use Subtract method:

DateTime myDate1 = new DateTime(1970, 1, 9, 0, 0, 00);
DateTime myDate2 = DateTime.Now;
TimeSpan ts = myDate2.Subtract(myDate1);
MessageBox.Show(ts.TotalSeconds.ToString());
Righteousness answered 13/8, 2012 at 7:16 Comment(0)
H
0
TimeSpan myDateResult;

myDateResult = DateTime.Now.Subtract(new DateTime(1970,1,9,0,0,00));
myDateResult.TotalSeconds.ToString();
Havelock answered 13/9, 2014 at 3:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.