« | »

Convert C# DateTime to unix timestamp

By Lennard | April 3, 2006

  1. DateTime now = new DateTime(2006, 3, 22, 23, 12, 23);
  2. TimeSpan t = (now - new DateTime(1970, 1, 1).toLocalTime());
  3. int timestamp = (int) t.TotalSeconds;

Topics: c# | 5 Comments »

5 Responses to “Convert C# DateTime to unix timestamp”

  1. Steven Says:
    April 5th, 2008 at 13:32

    Thanks for posting this – it’s exactly what I was looking for! :)

  2. jem777 Says:
    September 3rd, 2008 at 16:59

    What is “toLocalTime” ???

  3. josh Says:
    October 22nd, 2008 at 07:34

    It should be ToLocalTime (capitalized “to”)

  4. Freddy Says:
    May 15th, 2010 at 02:01

    Is better if you use ToUniversalTime to convert

  5. Joseph Says:
    March 2nd, 2011 at 17:29

    For GMT TIME use :

    int timestamp = (int)(DateTime.UtcNow – new DateTime(1970, 1, 1)).TotalSeconds;

    Works for me :)

Comments