Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# seconds since specific date

In C# 3.0, how do I get the seconds since 1/1/2010?

like image 627
craigmoliver Avatar asked Apr 16 '10 20:04

craigmoliver


2 Answers

Goes like this:

  TimeSpan test = DateTime.Now - new DateTime(2010, 01, 01);
  MessageBox.Show(test.TotalSeconds.ToString());

For one liner fun:

 MessageBox.Show((DateTime.Now - new DateTime(2010, 01, 01))
     .TotalSeconds.ToString());
like image 124
MadBoy Avatar answered Oct 05 '22 00:10

MadBoy


You can substract 2 DateTime instances and get a TimeSpan:

DateTime date = new DateTime(2010,1,1);
TimeSpan diff = DateTime.Now - date;
double seconds = diff.TotalSeconds;
like image 31
driis Avatar answered Oct 05 '22 01:10

driis