Get number of seconds since

25.6k views Asked by At

I am writing an application in C#, and I would love to know how you can get the number of seconds in each month. For example, today, February the 3th, I would love to have:

January: 2678400
February: 264000

Basically, I would love to know how many seconds in the past months and how many seconds in the current month at the current time (how many seconds so far).

Any code snippets would be appreciated....

3

There are 3 answers

0
Oded On

You can subtract two dates from each other and get the total seconds between them.

DateTime start = new DateTime(2011, 02, 03);
DateTime end = DateTime.Now;
var seconds = (start - end).TotalSeconds; 

The result of subtracting two dates from each other is a TimeSpan.

1
David Hedlund On

Subtracting one date from another will always give you a TimeSpan of the difference:

TimeSpan diff = (new DateTime(2011, 02, 10) - new DateTime(2011, 02, 01));

Console.WriteLine(diff.TotalSeconds);
0
Mark Avenius On

You can get the TotalSeconds property from a TimeSpan:

TimeSpan ts = DateTime.Now.Subtract(new DateTime(2011,2,1));
Console.Write(ts.TotalSeconds);

will give you the seconds so far this month.