How can I convert a Unix timestamp to DateTime and vice versa?









up vote
625
down vote

favorite
140












There is this example code, but then it starts talking about millisecond / nanosecond problems.



The same question is on MSDN, Seconds since the Unix epoch in C#.



This is what I've got so far:



public Double CreatedEpoch

get

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
TimeSpan span = (this.Created.ToLocalTime() - epoch);
return span.TotalSeconds;

set

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
this.Created = epoch.AddSeconds(value);











share|improve this question



















  • 77




    The upcoming .NET 4.6 (to be release later in this year) introduces support for this. See DateTimeOffset.FromUnixTimeSeconds and DateTimeOffset.ToUnixTimeSeconds methods. There are methods for millisecond unix-time as well.
    – Jeppe Stig Nielsen
    May 5 '15 at 9:29














up vote
625
down vote

favorite
140












There is this example code, but then it starts talking about millisecond / nanosecond problems.



The same question is on MSDN, Seconds since the Unix epoch in C#.



This is what I've got so far:



public Double CreatedEpoch

get

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
TimeSpan span = (this.Created.ToLocalTime() - epoch);
return span.TotalSeconds;

set

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
this.Created = epoch.AddSeconds(value);











share|improve this question



















  • 77




    The upcoming .NET 4.6 (to be release later in this year) introduces support for this. See DateTimeOffset.FromUnixTimeSeconds and DateTimeOffset.ToUnixTimeSeconds methods. There are methods for millisecond unix-time as well.
    – Jeppe Stig Nielsen
    May 5 '15 at 9:29












up vote
625
down vote

favorite
140









up vote
625
down vote

favorite
140






140





There is this example code, but then it starts talking about millisecond / nanosecond problems.



The same question is on MSDN, Seconds since the Unix epoch in C#.



This is what I've got so far:



public Double CreatedEpoch

get

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
TimeSpan span = (this.Created.ToLocalTime() - epoch);
return span.TotalSeconds;

set

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
this.Created = epoch.AddSeconds(value);











share|improve this question















There is this example code, but then it starts talking about millisecond / nanosecond problems.



The same question is on MSDN, Seconds since the Unix epoch in C#.



This is what I've got so far:



public Double CreatedEpoch

get

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
TimeSpan span = (this.Created.ToLocalTime() - epoch);
return span.TotalSeconds;

set

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
this.Created = epoch.AddSeconds(value);








c# datetime unix epoch data-conversion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 15 at 20:36









Peter Mortensen

13.3k1983111




13.3k1983111










asked Oct 30 '08 at 10:30









Mark Ingram

42.4k44149211




42.4k44149211







  • 77




    The upcoming .NET 4.6 (to be release later in this year) introduces support for this. See DateTimeOffset.FromUnixTimeSeconds and DateTimeOffset.ToUnixTimeSeconds methods. There are methods for millisecond unix-time as well.
    – Jeppe Stig Nielsen
    May 5 '15 at 9:29












  • 77




    The upcoming .NET 4.6 (to be release later in this year) introduces support for this. See DateTimeOffset.FromUnixTimeSeconds and DateTimeOffset.ToUnixTimeSeconds methods. There are methods for millisecond unix-time as well.
    – Jeppe Stig Nielsen
    May 5 '15 at 9:29







77




77




The upcoming .NET 4.6 (to be release later in this year) introduces support for this. See DateTimeOffset.FromUnixTimeSeconds and DateTimeOffset.ToUnixTimeSeconds methods. There are methods for millisecond unix-time as well.
– Jeppe Stig Nielsen
May 5 '15 at 9:29




The upcoming .NET 4.6 (to be release later in this year) introduces support for this. See DateTimeOffset.FromUnixTimeSeconds and DateTimeOffset.ToUnixTimeSeconds methods. There are methods for millisecond unix-time as well.
– Jeppe Stig Nielsen
May 5 '15 at 9:29












15 Answers
15






active

oldest

votes

















up vote
855
down vote



accepted










Here's what you need:



public static DateTime UnixTimeStampToDateTime( double unixTimeStamp )

// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
return dtDateTime;



Or, for Java (which is different because the timestamp is in milliseconds, not seconds):



public static DateTime JavaTimeStampToDateTime( double javaTimeStamp )

// Java timestamp is milliseconds past epoch
System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds( javaTimeStamp ).ToLocalTime();
return dtDateTime;






share|improve this answer


















  • 4




    Time in Windows is handled by HAL and only close-to-accurate within 1ms to 15ms. More info is available in Windows Internals around page 112, if anyone is interested.
    – Jim Schubert
    Apr 13 '12 at 14:59






  • 12




    This answer risks truncating the seconds... A double is a floating number. The argument should be int/long/etc.
    – ccook
    Mar 4 '13 at 14:59






  • 35




    These methods should accept a long or int, not a double. Also, for the Java time stamps, there is no need to divide by 1000 and round. Just do dtDateTime.AddMilliseconds(javaTimeStamp).ToLocalTime();
    – Justin Johnson
    Feb 21 '14 at 21:20






  • 11




    Did you just miss the "vice versa"? How do we convert a DateTime to a timestamp?
    – Jonny
    Dec 8 '14 at 8:31






  • 20




    For the .NET Framework 4.6 and above there is now static DateTimeOffset.FromUnixMilliseconds and DateTimeOffset.ToUnixMilliseconds.
    – rookie1024
    May 18 '16 at 2:49

















up vote
287
down vote













The latest version of .NET (v4.6) has added built-in support for Unix time conversions. That includes both to and from Unix time represented by either seconds or milliseconds.



  • Unix time in seconds to UTC DateTimeOffset:



DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(1000);



  • DateTimeOffset to Unix time in seconds:



long unixTimeStampInSeconds = dateTimeOffset.ToUnixTimeSeconds();


  • Unix time in milliseconds to UTC DateTimeOffset:



DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(1000000);



  • DateTimeOffset to Unix time in milliseconds:



long unixTimeStampInMilliseconds = dateTimeOffset.ToUnixTimeMilliseconds();



Note: These methods convert to and from a UTC DateTimeOffset. To get a DateTime representation simply use the DateTimeOffset.UtcDateTime or DateTimeOffset.LocalDateTime properties:



DateTime dateTime = dateTimeOffset.UtcDateTime;





share|improve this answer


















  • 1




    docs.microsoft.com/en-us/dotnet/api/…
    – yedevtxt
    Jul 12 '17 at 16:35










  • This does not convert time to local time. You get UTC if you use DateTimeOffset.FromUnixTimeSeconds().
    – Berend de Boer
    Aug 27 '17 at 23:56






  • 2




    @BerenddeBoer You can use ToLocalTime if you want.
    – i3arnon
    Aug 28 '17 at 17:11

















up vote
207
down vote













DateTime to UNIX timestamp:



public static double DateTimeToUnixTimestamp(DateTime dateTime)

return (TimeZoneInfo.ConvertTimeToUtc(dateTime) -
new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds;






share|improve this answer





























    up vote
    44
    down vote













    "UTC does not change with a change of seasons, but local time or civil time may change if a time zone jurisdiction observes daylight saving time (summer time). For example, UTC is 5 hours ahead of (that is, later in the day than) local time on the east coast of the United States during winter, but 4 hours ahead while daylight saving is observed there."



    So this is my code:



    TimeSpan span = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0,DateTimeKind.Utc));
    double unixTime = span.TotalSeconds;





    share|improve this answer
















    • 2




      but this returns a double, I guess one needs to cast to long?
      – knocte
      May 6 '13 at 6:25

















    up vote
    18
    down vote













    Be careful, if you need precision higher than milliseconds!



    .NET (v4.6) methods (e.g. FromUnixTimeMilliseconds) don't provide this precision.



    AddSeconds and AddMilliseconds also cut off the microseconds in the double.



    These versions have high precision:



    Unix -> DateTime



    public static DateTime UnixTimestampToDateTime(double unixTime)

    DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
    long unixTimeStampInTicks = (long) (unixTime * TimeSpan.TicksPerSecond);
    return new DateTime(unixStart.Ticks + unixTimeStampInTicks, System.DateTimeKind.Utc);



    DateTime -> Unix



    public static double DateTimeToUnixTimestamp(DateTime dateTime)

    DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
    long unixTimeStampInTicks = (dateTime.ToUniversalTime() - unixStart).Ticks;
    return (double) unixTimeStampInTicks / TimeSpan.TicksPerSecond;






    share|improve this answer


















    • 2




      This is the correct answer. The others get the time zone incorrect on the convert back from timestamp.
      – IamIC
      Jun 20 '17 at 10:10










    • for DateTime->Java, just [code] return (long) unixTimeStampInTicks / TimeSpan.TicksPerMilliSecond; [/code]
      – Max
      Feb 17 at 18:07











    • So in your UnixTimestampToDateTime, the supplied unixTime is still in seconds, right?
      – Ngoc Pham
      May 3 at 21:21










    • @NgocPham yes it is
      – Felix Keil
      May 4 at 4:56

















    up vote
    12
    down vote













    See IdentityModel.EpochTimeExtensions



    public static class EpochTimeExtensions

    /// <summary>
    /// Converts the given date value to epoch time.
    /// </summary>
    public static long ToEpochTime(this DateTime dateTime)

    var date = dateTime.ToUniversalTime();
    var ticks = date.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks;
    var ts = ticks / TimeSpan.TicksPerSecond;
    return ts;


    /// <summary>
    /// Converts the given date value to epoch time.
    /// </summary>
    public static long ToEpochTime(this DateTimeOffset dateTime)

    var date = dateTime.ToUniversalTime();
    var ticks = date.Ticks - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).Ticks;
    var ts = ticks / TimeSpan.TicksPerSecond;
    return ts;


    /// <summary>
    /// Converts the given epoch time to a <see cref="DateTime"/> with <see cref="DateTimeKind.Utc"/> kind.
    /// </summary>
    public static DateTime ToDateTimeFromEpoch(this long intDate)

    var timeInTicks = intDate * TimeSpan.TicksPerSecond;
    return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddTicks(timeInTicks);


    /// <summary>
    /// Converts the given epoch time to a UTC <see cref="DateTimeOffset"/>.
    /// </summary>
    public static DateTimeOffset ToDateTimeOffsetFromEpoch(this long intDate)

    var timeInTicks = intDate * TimeSpan.TicksPerSecond;
    return new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddTicks(timeInTicks);







    share|improve this answer





























      up vote
      10
      down vote













      To supplement ScottCher's answer, I recently found myself in the annoying scenario of having both seconds and milliseconds UNIX timestamps arbitrarily mixed together in an input data set. The following code seems to handle this well:



      static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
      static readonly double MaxUnixSeconds = (DateTime.MaxValue - UnixEpoch).TotalSeconds;

      public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)

      return unixTimeStamp > MaxUnixSeconds
      ? UnixEpoch.AddMilliseconds(unixTimeStamp)
      : UnixEpoch.AddSeconds(unixTimeStamp);






      share|improve this answer
















      • 1




        Be careful when not using the DateTimeKind argument, since the constructed DateTime will be in the computer's local time (thanks for the code, Chris)!
        – Sam Grondahl
        Sep 16 '13 at 22:40










      • Beware - this will not work for unix timestamps for dates prior to the 11th of January 1978 if they're represented in milliseconds. A unix datestamp of 253324800 (seconds) gives the correct date of 11.01.1978, whereas the millisecond representation 253324800000 gives a date of 18.07.9997. This may have worked for your data set, but it's not a general solution.
        – Øyvind
        May 17 at 7:24

















      up vote
      6
      down vote













      Unix time conversion is new in .NET Framework 4.6.



      You can now more easily convert date and time values to or from .NET Framework types and Unix time. This can be necessary, for example, when converting time values between a JavaScript client and .NET server. The following APIs have been added to the DateTimeOffset structure:



      static DateTimeOffset FromUnixTimeSeconds(long seconds)
      static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)
      long DateTimeOffset.ToUnixTimeSeconds()
      long DateTimeOffset.ToUnixTimeMilliseconds()





      share|improve this answer




















      • This does not give you local time, you get UTC.
        – Berend de Boer
        Aug 27 '17 at 23:57










      • @BerenddeBoer That's a reasonable default. You may apply a custom offset after that as you want.
        – Deilan
        Nov 21 '17 at 13:04






      • 1




        @BerenddeBoer That misunderstands what unix time is. Unix time is seconds since midnight, Jan 1, 1970, UTC. Doesn't matter where you are, the number of seconds since that epoch doesn't change. Converting it to human readable local time displays are separate from this universal representation, as it should be.
        – Tanktalus
        Sep 11 at 17:18

















      up vote
      4
      down vote













      I found the right answer just by comparing the conversion to 1/1/1970 w/o the local time adjustment;



      DateTime date = new DateTime(2011, 4, 1, 12, 0, 0, 0);
      DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
      TimeSpan span = (date - epoch);
      double unixTime =span.TotalSeconds;





      share|improve this answer





























        up vote
        3
        down vote













        DateTime unixEpoch = DateTime.ParseExact("1970-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
        DateTime convertedTime = unixEpoch.AddMilliseconds(unixTimeInMillisconds);


        Of course, one can make unixEpoch a global static, so it only needs to appear once in your project, and one can use AddSeconds if the UNIX time is in seconds.



        To go the other way:



        double unixTimeInMilliseconds = timeToConvert.Subtract(unixEpoch).TotalMilliseconds;


        Truncate to Int64 and/or use TotalSeconds as needed.






        share|improve this answer



























          up vote
          2
          down vote













          A Unix tick is 1 second (if I remember well), and a .NET tick is 100 nanoseconds.



          If you've been encountering problems with nanoseconds, you might want to try using AddTick(10000000 * value).






          share|improve this answer


















          • 3




            Unix is seconds past epoch - which is 1/1/70.
            – ScottCher
            Oct 30 '08 at 14:44

















          up vote
          1
          down vote













          I needed to convert a timeval struct (seconds, microseconds) containing UNIX time to DateTime without losing precision and haven't found an answer here so I thought I just might add mine:



          DateTime _epochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
          private DateTime UnixTimeToDateTime(Timeval unixTime)

          return _epochTime.AddTicks(
          unixTime.Seconds * TimeSpan.TicksPerSecond +
          unixTime.Microseconds * TimeSpan.TicksPerMillisecond/1000);






          share|improve this answer



























            up vote
            0
            down vote













            public static class UnixTime

            private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);

            public static DateTime UnixTimeToDateTime(double unixTimeStamp)

            return Epoch.AddSeconds(unixTimeStamp).ToUniversalTime();




            you can call UnixTime.UnixTimeToDateTime(double datetime))






            share|improve this answer





























              up vote
              0
              down vote













              var dt = DateTime.Now; 
              var unixTime = ((DateTimeOffset)dt).ToUnixTimeSeconds();


              // 1510396991



              var dt = DateTimeOffset.FromUnixTimeSeconds(1510396991);


              // [11.11.2017 10:43:11 +00:00]






              share|improve this answer



























                up vote
                -2
                down vote













                For .NET 4.6 and later:



                public static class UnixDateTime

                public static DateTimeOffset FromUnixTimeSeconds(long seconds)


                public static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)

                if (milliseconds < -62135596800000L

                public static long ToUnixTimeSeconds(this DateTimeOffset utcDateTime)

                return utcDateTime.Ticks / 10000000L - 62135596800L;


                public static long ToUnixTimeMilliseconds(this DateTimeOffset utcDateTime)

                return utcDateTime.Ticks / 10000L - 62135596800000L;


                [Test]
                public void UnixSeconds()

                DateTime utcNow = DateTime.UtcNow;
                DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

                long unixTimestampInSeconds = utcNowOffset.ToUnixTimeSeconds();

                DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeSeconds(unixTimestampInSeconds);

                Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
                Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
                Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
                Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
                Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
                Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);


                [Test]
                public void UnixMilliseconds()

                DateTime utcNow = DateTime.UtcNow;
                DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

                long unixTimestampInMilliseconds = utcNowOffset.ToUnixTimeMilliseconds();

                DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeMilliseconds(unixTimestampInMilliseconds);

                Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
                Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
                Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
                Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
                Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
                Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);
                Assert.AreEqual(utcNowOffset.Millisecond, utcNowOffsetTest.Millisecond);







                share|improve this answer


















                • 4




                  I do not understand. In .NET 4.6, the BCL already has those methods (see e.g. my comment to the question above, or some of the other new answers (2015). So what should the point be in writing them again? Did you mean that your answer was a solution for versions prior to 4.6?
                  – Jeppe Stig Nielsen
                  Feb 19 '16 at 10:32









                protected by i3arnon Nov 27 '14 at 7:44



                Thank you for your interest in this question.
                Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                Would you like to answer one of these unanswered questions instead?














                15 Answers
                15






                active

                oldest

                votes








                15 Answers
                15






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                855
                down vote



                accepted










                Here's what you need:



                public static DateTime UnixTimeStampToDateTime( double unixTimeStamp )

                // Unix timestamp is seconds past epoch
                System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
                dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
                return dtDateTime;



                Or, for Java (which is different because the timestamp is in milliseconds, not seconds):



                public static DateTime JavaTimeStampToDateTime( double javaTimeStamp )

                // Java timestamp is milliseconds past epoch
                System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
                dtDateTime = dtDateTime.AddMilliseconds( javaTimeStamp ).ToLocalTime();
                return dtDateTime;






                share|improve this answer


















                • 4




                  Time in Windows is handled by HAL and only close-to-accurate within 1ms to 15ms. More info is available in Windows Internals around page 112, if anyone is interested.
                  – Jim Schubert
                  Apr 13 '12 at 14:59






                • 12




                  This answer risks truncating the seconds... A double is a floating number. The argument should be int/long/etc.
                  – ccook
                  Mar 4 '13 at 14:59






                • 35




                  These methods should accept a long or int, not a double. Also, for the Java time stamps, there is no need to divide by 1000 and round. Just do dtDateTime.AddMilliseconds(javaTimeStamp).ToLocalTime();
                  – Justin Johnson
                  Feb 21 '14 at 21:20






                • 11




                  Did you just miss the "vice versa"? How do we convert a DateTime to a timestamp?
                  – Jonny
                  Dec 8 '14 at 8:31






                • 20




                  For the .NET Framework 4.6 and above there is now static DateTimeOffset.FromUnixMilliseconds and DateTimeOffset.ToUnixMilliseconds.
                  – rookie1024
                  May 18 '16 at 2:49














                up vote
                855
                down vote



                accepted










                Here's what you need:



                public static DateTime UnixTimeStampToDateTime( double unixTimeStamp )

                // Unix timestamp is seconds past epoch
                System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
                dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
                return dtDateTime;



                Or, for Java (which is different because the timestamp is in milliseconds, not seconds):



                public static DateTime JavaTimeStampToDateTime( double javaTimeStamp )

                // Java timestamp is milliseconds past epoch
                System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
                dtDateTime = dtDateTime.AddMilliseconds( javaTimeStamp ).ToLocalTime();
                return dtDateTime;






                share|improve this answer


















                • 4




                  Time in Windows is handled by HAL and only close-to-accurate within 1ms to 15ms. More info is available in Windows Internals around page 112, if anyone is interested.
                  – Jim Schubert
                  Apr 13 '12 at 14:59






                • 12




                  This answer risks truncating the seconds... A double is a floating number. The argument should be int/long/etc.
                  – ccook
                  Mar 4 '13 at 14:59






                • 35




                  These methods should accept a long or int, not a double. Also, for the Java time stamps, there is no need to divide by 1000 and round. Just do dtDateTime.AddMilliseconds(javaTimeStamp).ToLocalTime();
                  – Justin Johnson
                  Feb 21 '14 at 21:20






                • 11




                  Did you just miss the "vice versa"? How do we convert a DateTime to a timestamp?
                  – Jonny
                  Dec 8 '14 at 8:31






                • 20




                  For the .NET Framework 4.6 and above there is now static DateTimeOffset.FromUnixMilliseconds and DateTimeOffset.ToUnixMilliseconds.
                  – rookie1024
                  May 18 '16 at 2:49












                up vote
                855
                down vote



                accepted







                up vote
                855
                down vote



                accepted






                Here's what you need:



                public static DateTime UnixTimeStampToDateTime( double unixTimeStamp )

                // Unix timestamp is seconds past epoch
                System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
                dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
                return dtDateTime;



                Or, for Java (which is different because the timestamp is in milliseconds, not seconds):



                public static DateTime JavaTimeStampToDateTime( double javaTimeStamp )

                // Java timestamp is milliseconds past epoch
                System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
                dtDateTime = dtDateTime.AddMilliseconds( javaTimeStamp ).ToLocalTime();
                return dtDateTime;






                share|improve this answer














                Here's what you need:



                public static DateTime UnixTimeStampToDateTime( double unixTimeStamp )

                // Unix timestamp is seconds past epoch
                System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
                dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
                return dtDateTime;



                Or, for Java (which is different because the timestamp is in milliseconds, not seconds):



                public static DateTime JavaTimeStampToDateTime( double javaTimeStamp )

                // Java timestamp is milliseconds past epoch
                System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
                dtDateTime = dtDateTime.AddMilliseconds( javaTimeStamp ).ToLocalTime();
                return dtDateTime;







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jul 25 '17 at 11:12









                K48

                5,27183988




                5,27183988










                answered Oct 30 '08 at 14:42









                ScottCher

                10.4k52225




                10.4k52225







                • 4




                  Time in Windows is handled by HAL and only close-to-accurate within 1ms to 15ms. More info is available in Windows Internals around page 112, if anyone is interested.
                  – Jim Schubert
                  Apr 13 '12 at 14:59






                • 12




                  This answer risks truncating the seconds... A double is a floating number. The argument should be int/long/etc.
                  – ccook
                  Mar 4 '13 at 14:59






                • 35




                  These methods should accept a long or int, not a double. Also, for the Java time stamps, there is no need to divide by 1000 and round. Just do dtDateTime.AddMilliseconds(javaTimeStamp).ToLocalTime();
                  – Justin Johnson
                  Feb 21 '14 at 21:20






                • 11




                  Did you just miss the "vice versa"? How do we convert a DateTime to a timestamp?
                  – Jonny
                  Dec 8 '14 at 8:31






                • 20




                  For the .NET Framework 4.6 and above there is now static DateTimeOffset.FromUnixMilliseconds and DateTimeOffset.ToUnixMilliseconds.
                  – rookie1024
                  May 18 '16 at 2:49












                • 4




                  Time in Windows is handled by HAL and only close-to-accurate within 1ms to 15ms. More info is available in Windows Internals around page 112, if anyone is interested.
                  – Jim Schubert
                  Apr 13 '12 at 14:59






                • 12




                  This answer risks truncating the seconds... A double is a floating number. The argument should be int/long/etc.
                  – ccook
                  Mar 4 '13 at 14:59






                • 35




                  These methods should accept a long or int, not a double. Also, for the Java time stamps, there is no need to divide by 1000 and round. Just do dtDateTime.AddMilliseconds(javaTimeStamp).ToLocalTime();
                  – Justin Johnson
                  Feb 21 '14 at 21:20






                • 11




                  Did you just miss the "vice versa"? How do we convert a DateTime to a timestamp?
                  – Jonny
                  Dec 8 '14 at 8:31






                • 20




                  For the .NET Framework 4.6 and above there is now static DateTimeOffset.FromUnixMilliseconds and DateTimeOffset.ToUnixMilliseconds.
                  – rookie1024
                  May 18 '16 at 2:49







                4




                4




                Time in Windows is handled by HAL and only close-to-accurate within 1ms to 15ms. More info is available in Windows Internals around page 112, if anyone is interested.
                – Jim Schubert
                Apr 13 '12 at 14:59




                Time in Windows is handled by HAL and only close-to-accurate within 1ms to 15ms. More info is available in Windows Internals around page 112, if anyone is interested.
                – Jim Schubert
                Apr 13 '12 at 14:59




                12




                12




                This answer risks truncating the seconds... A double is a floating number. The argument should be int/long/etc.
                – ccook
                Mar 4 '13 at 14:59




                This answer risks truncating the seconds... A double is a floating number. The argument should be int/long/etc.
                – ccook
                Mar 4 '13 at 14:59




                35




                35




                These methods should accept a long or int, not a double. Also, for the Java time stamps, there is no need to divide by 1000 and round. Just do dtDateTime.AddMilliseconds(javaTimeStamp).ToLocalTime();
                – Justin Johnson
                Feb 21 '14 at 21:20




                These methods should accept a long or int, not a double. Also, for the Java time stamps, there is no need to divide by 1000 and round. Just do dtDateTime.AddMilliseconds(javaTimeStamp).ToLocalTime();
                – Justin Johnson
                Feb 21 '14 at 21:20




                11




                11




                Did you just miss the "vice versa"? How do we convert a DateTime to a timestamp?
                – Jonny
                Dec 8 '14 at 8:31




                Did you just miss the "vice versa"? How do we convert a DateTime to a timestamp?
                – Jonny
                Dec 8 '14 at 8:31




                20




                20




                For the .NET Framework 4.6 and above there is now static DateTimeOffset.FromUnixMilliseconds and DateTimeOffset.ToUnixMilliseconds.
                – rookie1024
                May 18 '16 at 2:49




                For the .NET Framework 4.6 and above there is now static DateTimeOffset.FromUnixMilliseconds and DateTimeOffset.ToUnixMilliseconds.
                – rookie1024
                May 18 '16 at 2:49












                up vote
                287
                down vote













                The latest version of .NET (v4.6) has added built-in support for Unix time conversions. That includes both to and from Unix time represented by either seconds or milliseconds.



                • Unix time in seconds to UTC DateTimeOffset:



                DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(1000);



                • DateTimeOffset to Unix time in seconds:



                long unixTimeStampInSeconds = dateTimeOffset.ToUnixTimeSeconds();


                • Unix time in milliseconds to UTC DateTimeOffset:



                DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(1000000);



                • DateTimeOffset to Unix time in milliseconds:



                long unixTimeStampInMilliseconds = dateTimeOffset.ToUnixTimeMilliseconds();



                Note: These methods convert to and from a UTC DateTimeOffset. To get a DateTime representation simply use the DateTimeOffset.UtcDateTime or DateTimeOffset.LocalDateTime properties:



                DateTime dateTime = dateTimeOffset.UtcDateTime;





                share|improve this answer


















                • 1




                  docs.microsoft.com/en-us/dotnet/api/…
                  – yedevtxt
                  Jul 12 '17 at 16:35










                • This does not convert time to local time. You get UTC if you use DateTimeOffset.FromUnixTimeSeconds().
                  – Berend de Boer
                  Aug 27 '17 at 23:56






                • 2




                  @BerenddeBoer You can use ToLocalTime if you want.
                  – i3arnon
                  Aug 28 '17 at 17:11














                up vote
                287
                down vote













                The latest version of .NET (v4.6) has added built-in support for Unix time conversions. That includes both to and from Unix time represented by either seconds or milliseconds.



                • Unix time in seconds to UTC DateTimeOffset:



                DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(1000);



                • DateTimeOffset to Unix time in seconds:



                long unixTimeStampInSeconds = dateTimeOffset.ToUnixTimeSeconds();


                • Unix time in milliseconds to UTC DateTimeOffset:



                DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(1000000);



                • DateTimeOffset to Unix time in milliseconds:



                long unixTimeStampInMilliseconds = dateTimeOffset.ToUnixTimeMilliseconds();



                Note: These methods convert to and from a UTC DateTimeOffset. To get a DateTime representation simply use the DateTimeOffset.UtcDateTime or DateTimeOffset.LocalDateTime properties:



                DateTime dateTime = dateTimeOffset.UtcDateTime;





                share|improve this answer


















                • 1




                  docs.microsoft.com/en-us/dotnet/api/…
                  – yedevtxt
                  Jul 12 '17 at 16:35










                • This does not convert time to local time. You get UTC if you use DateTimeOffset.FromUnixTimeSeconds().
                  – Berend de Boer
                  Aug 27 '17 at 23:56






                • 2




                  @BerenddeBoer You can use ToLocalTime if you want.
                  – i3arnon
                  Aug 28 '17 at 17:11












                up vote
                287
                down vote










                up vote
                287
                down vote









                The latest version of .NET (v4.6) has added built-in support for Unix time conversions. That includes both to and from Unix time represented by either seconds or milliseconds.



                • Unix time in seconds to UTC DateTimeOffset:



                DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(1000);



                • DateTimeOffset to Unix time in seconds:



                long unixTimeStampInSeconds = dateTimeOffset.ToUnixTimeSeconds();


                • Unix time in milliseconds to UTC DateTimeOffset:



                DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(1000000);



                • DateTimeOffset to Unix time in milliseconds:



                long unixTimeStampInMilliseconds = dateTimeOffset.ToUnixTimeMilliseconds();



                Note: These methods convert to and from a UTC DateTimeOffset. To get a DateTime representation simply use the DateTimeOffset.UtcDateTime or DateTimeOffset.LocalDateTime properties:



                DateTime dateTime = dateTimeOffset.UtcDateTime;





                share|improve this answer














                The latest version of .NET (v4.6) has added built-in support for Unix time conversions. That includes both to and from Unix time represented by either seconds or milliseconds.



                • Unix time in seconds to UTC DateTimeOffset:



                DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(1000);



                • DateTimeOffset to Unix time in seconds:



                long unixTimeStampInSeconds = dateTimeOffset.ToUnixTimeSeconds();


                • Unix time in milliseconds to UTC DateTimeOffset:



                DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(1000000);



                • DateTimeOffset to Unix time in milliseconds:



                long unixTimeStampInMilliseconds = dateTimeOffset.ToUnixTimeMilliseconds();



                Note: These methods convert to and from a UTC DateTimeOffset. To get a DateTime representation simply use the DateTimeOffset.UtcDateTime or DateTimeOffset.LocalDateTime properties:



                DateTime dateTime = dateTimeOffset.UtcDateTime;






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 21 at 2:16









                samlh

                32




                32










                answered Oct 6 '14 at 22:17









                i3arnon

                78.8k20211254




                78.8k20211254







                • 1




                  docs.microsoft.com/en-us/dotnet/api/…
                  – yedevtxt
                  Jul 12 '17 at 16:35










                • This does not convert time to local time. You get UTC if you use DateTimeOffset.FromUnixTimeSeconds().
                  – Berend de Boer
                  Aug 27 '17 at 23:56






                • 2




                  @BerenddeBoer You can use ToLocalTime if you want.
                  – i3arnon
                  Aug 28 '17 at 17:11












                • 1




                  docs.microsoft.com/en-us/dotnet/api/…
                  – yedevtxt
                  Jul 12 '17 at 16:35










                • This does not convert time to local time. You get UTC if you use DateTimeOffset.FromUnixTimeSeconds().
                  – Berend de Boer
                  Aug 27 '17 at 23:56






                • 2




                  @BerenddeBoer You can use ToLocalTime if you want.
                  – i3arnon
                  Aug 28 '17 at 17:11







                1




                1




                docs.microsoft.com/en-us/dotnet/api/…
                – yedevtxt
                Jul 12 '17 at 16:35




                docs.microsoft.com/en-us/dotnet/api/…
                – yedevtxt
                Jul 12 '17 at 16:35












                This does not convert time to local time. You get UTC if you use DateTimeOffset.FromUnixTimeSeconds().
                – Berend de Boer
                Aug 27 '17 at 23:56




                This does not convert time to local time. You get UTC if you use DateTimeOffset.FromUnixTimeSeconds().
                – Berend de Boer
                Aug 27 '17 at 23:56




                2




                2




                @BerenddeBoer You can use ToLocalTime if you want.
                – i3arnon
                Aug 28 '17 at 17:11




                @BerenddeBoer You can use ToLocalTime if you want.
                – i3arnon
                Aug 28 '17 at 17:11










                up vote
                207
                down vote













                DateTime to UNIX timestamp:



                public static double DateTimeToUnixTimestamp(DateTime dateTime)

                return (TimeZoneInfo.ConvertTimeToUtc(dateTime) -
                new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds;






                share|improve this answer


























                  up vote
                  207
                  down vote













                  DateTime to UNIX timestamp:



                  public static double DateTimeToUnixTimestamp(DateTime dateTime)

                  return (TimeZoneInfo.ConvertTimeToUtc(dateTime) -
                  new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds;






                  share|improve this answer
























                    up vote
                    207
                    down vote










                    up vote
                    207
                    down vote









                    DateTime to UNIX timestamp:



                    public static double DateTimeToUnixTimestamp(DateTime dateTime)

                    return (TimeZoneInfo.ConvertTimeToUtc(dateTime) -
                    new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds;






                    share|improve this answer














                    DateTime to UNIX timestamp:



                    public static double DateTimeToUnixTimestamp(DateTime dateTime)

                    return (TimeZoneInfo.ConvertTimeToUtc(dateTime) -
                    new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds;







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 23 '16 at 15:08









                    Nasreddine

                    25.5k156083




                    25.5k156083










                    answered Sep 29 '11 at 11:12









                    Dmitry Fedorkov

                    3,51111425




                    3,51111425




















                        up vote
                        44
                        down vote













                        "UTC does not change with a change of seasons, but local time or civil time may change if a time zone jurisdiction observes daylight saving time (summer time). For example, UTC is 5 hours ahead of (that is, later in the day than) local time on the east coast of the United States during winter, but 4 hours ahead while daylight saving is observed there."



                        So this is my code:



                        TimeSpan span = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0,DateTimeKind.Utc));
                        double unixTime = span.TotalSeconds;





                        share|improve this answer
















                        • 2




                          but this returns a double, I guess one needs to cast to long?
                          – knocte
                          May 6 '13 at 6:25














                        up vote
                        44
                        down vote













                        "UTC does not change with a change of seasons, but local time or civil time may change if a time zone jurisdiction observes daylight saving time (summer time). For example, UTC is 5 hours ahead of (that is, later in the day than) local time on the east coast of the United States during winter, but 4 hours ahead while daylight saving is observed there."



                        So this is my code:



                        TimeSpan span = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0,DateTimeKind.Utc));
                        double unixTime = span.TotalSeconds;





                        share|improve this answer
















                        • 2




                          but this returns a double, I guess one needs to cast to long?
                          – knocte
                          May 6 '13 at 6:25












                        up vote
                        44
                        down vote










                        up vote
                        44
                        down vote









                        "UTC does not change with a change of seasons, but local time or civil time may change if a time zone jurisdiction observes daylight saving time (summer time). For example, UTC is 5 hours ahead of (that is, later in the day than) local time on the east coast of the United States during winter, but 4 hours ahead while daylight saving is observed there."



                        So this is my code:



                        TimeSpan span = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0,DateTimeKind.Utc));
                        double unixTime = span.TotalSeconds;





                        share|improve this answer












                        "UTC does not change with a change of seasons, but local time or civil time may change if a time zone jurisdiction observes daylight saving time (summer time). For example, UTC is 5 hours ahead of (that is, later in the day than) local time on the east coast of the United States during winter, but 4 hours ahead while daylight saving is observed there."



                        So this is my code:



                        TimeSpan span = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0,DateTimeKind.Utc));
                        double unixTime = span.TotalSeconds;






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Apr 13 '12 at 19:55









                        gl051

                        52147




                        52147







                        • 2




                          but this returns a double, I guess one needs to cast to long?
                          – knocte
                          May 6 '13 at 6:25












                        • 2




                          but this returns a double, I guess one needs to cast to long?
                          – knocte
                          May 6 '13 at 6:25







                        2




                        2




                        but this returns a double, I guess one needs to cast to long?
                        – knocte
                        May 6 '13 at 6:25




                        but this returns a double, I guess one needs to cast to long?
                        – knocte
                        May 6 '13 at 6:25










                        up vote
                        18
                        down vote













                        Be careful, if you need precision higher than milliseconds!



                        .NET (v4.6) methods (e.g. FromUnixTimeMilliseconds) don't provide this precision.



                        AddSeconds and AddMilliseconds also cut off the microseconds in the double.



                        These versions have high precision:



                        Unix -> DateTime



                        public static DateTime UnixTimestampToDateTime(double unixTime)

                        DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                        long unixTimeStampInTicks = (long) (unixTime * TimeSpan.TicksPerSecond);
                        return new DateTime(unixStart.Ticks + unixTimeStampInTicks, System.DateTimeKind.Utc);



                        DateTime -> Unix



                        public static double DateTimeToUnixTimestamp(DateTime dateTime)

                        DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                        long unixTimeStampInTicks = (dateTime.ToUniversalTime() - unixStart).Ticks;
                        return (double) unixTimeStampInTicks / TimeSpan.TicksPerSecond;






                        share|improve this answer


















                        • 2




                          This is the correct answer. The others get the time zone incorrect on the convert back from timestamp.
                          – IamIC
                          Jun 20 '17 at 10:10










                        • for DateTime->Java, just [code] return (long) unixTimeStampInTicks / TimeSpan.TicksPerMilliSecond; [/code]
                          – Max
                          Feb 17 at 18:07











                        • So in your UnixTimestampToDateTime, the supplied unixTime is still in seconds, right?
                          – Ngoc Pham
                          May 3 at 21:21










                        • @NgocPham yes it is
                          – Felix Keil
                          May 4 at 4:56














                        up vote
                        18
                        down vote













                        Be careful, if you need precision higher than milliseconds!



                        .NET (v4.6) methods (e.g. FromUnixTimeMilliseconds) don't provide this precision.



                        AddSeconds and AddMilliseconds also cut off the microseconds in the double.



                        These versions have high precision:



                        Unix -> DateTime



                        public static DateTime UnixTimestampToDateTime(double unixTime)

                        DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                        long unixTimeStampInTicks = (long) (unixTime * TimeSpan.TicksPerSecond);
                        return new DateTime(unixStart.Ticks + unixTimeStampInTicks, System.DateTimeKind.Utc);



                        DateTime -> Unix



                        public static double DateTimeToUnixTimestamp(DateTime dateTime)

                        DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                        long unixTimeStampInTicks = (dateTime.ToUniversalTime() - unixStart).Ticks;
                        return (double) unixTimeStampInTicks / TimeSpan.TicksPerSecond;






                        share|improve this answer


















                        • 2




                          This is the correct answer. The others get the time zone incorrect on the convert back from timestamp.
                          – IamIC
                          Jun 20 '17 at 10:10










                        • for DateTime->Java, just [code] return (long) unixTimeStampInTicks / TimeSpan.TicksPerMilliSecond; [/code]
                          – Max
                          Feb 17 at 18:07











                        • So in your UnixTimestampToDateTime, the supplied unixTime is still in seconds, right?
                          – Ngoc Pham
                          May 3 at 21:21










                        • @NgocPham yes it is
                          – Felix Keil
                          May 4 at 4:56












                        up vote
                        18
                        down vote










                        up vote
                        18
                        down vote









                        Be careful, if you need precision higher than milliseconds!



                        .NET (v4.6) methods (e.g. FromUnixTimeMilliseconds) don't provide this precision.



                        AddSeconds and AddMilliseconds also cut off the microseconds in the double.



                        These versions have high precision:



                        Unix -> DateTime



                        public static DateTime UnixTimestampToDateTime(double unixTime)

                        DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                        long unixTimeStampInTicks = (long) (unixTime * TimeSpan.TicksPerSecond);
                        return new DateTime(unixStart.Ticks + unixTimeStampInTicks, System.DateTimeKind.Utc);



                        DateTime -> Unix



                        public static double DateTimeToUnixTimestamp(DateTime dateTime)

                        DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                        long unixTimeStampInTicks = (dateTime.ToUniversalTime() - unixStart).Ticks;
                        return (double) unixTimeStampInTicks / TimeSpan.TicksPerSecond;






                        share|improve this answer














                        Be careful, if you need precision higher than milliseconds!



                        .NET (v4.6) methods (e.g. FromUnixTimeMilliseconds) don't provide this precision.



                        AddSeconds and AddMilliseconds also cut off the microseconds in the double.



                        These versions have high precision:



                        Unix -> DateTime



                        public static DateTime UnixTimestampToDateTime(double unixTime)

                        DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                        long unixTimeStampInTicks = (long) (unixTime * TimeSpan.TicksPerSecond);
                        return new DateTime(unixStart.Ticks + unixTimeStampInTicks, System.DateTimeKind.Utc);



                        DateTime -> Unix



                        public static double DateTimeToUnixTimestamp(DateTime dateTime)

                        DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                        long unixTimeStampInTicks = (dateTime.ToUniversalTime() - unixStart).Ticks;
                        return (double) unixTimeStampInTicks / TimeSpan.TicksPerSecond;







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Feb 19 at 8:50

























                        answered Jul 23 '14 at 8:57









                        Felix Keil

                        1,3881318




                        1,3881318







                        • 2




                          This is the correct answer. The others get the time zone incorrect on the convert back from timestamp.
                          – IamIC
                          Jun 20 '17 at 10:10










                        • for DateTime->Java, just [code] return (long) unixTimeStampInTicks / TimeSpan.TicksPerMilliSecond; [/code]
                          – Max
                          Feb 17 at 18:07











                        • So in your UnixTimestampToDateTime, the supplied unixTime is still in seconds, right?
                          – Ngoc Pham
                          May 3 at 21:21










                        • @NgocPham yes it is
                          – Felix Keil
                          May 4 at 4:56












                        • 2




                          This is the correct answer. The others get the time zone incorrect on the convert back from timestamp.
                          – IamIC
                          Jun 20 '17 at 10:10










                        • for DateTime->Java, just [code] return (long) unixTimeStampInTicks / TimeSpan.TicksPerMilliSecond; [/code]
                          – Max
                          Feb 17 at 18:07











                        • So in your UnixTimestampToDateTime, the supplied unixTime is still in seconds, right?
                          – Ngoc Pham
                          May 3 at 21:21










                        • @NgocPham yes it is
                          – Felix Keil
                          May 4 at 4:56







                        2




                        2




                        This is the correct answer. The others get the time zone incorrect on the convert back from timestamp.
                        – IamIC
                        Jun 20 '17 at 10:10




                        This is the correct answer. The others get the time zone incorrect on the convert back from timestamp.
                        – IamIC
                        Jun 20 '17 at 10:10












                        for DateTime->Java, just [code] return (long) unixTimeStampInTicks / TimeSpan.TicksPerMilliSecond; [/code]
                        – Max
                        Feb 17 at 18:07





                        for DateTime->Java, just [code] return (long) unixTimeStampInTicks / TimeSpan.TicksPerMilliSecond; [/code]
                        – Max
                        Feb 17 at 18:07













                        So in your UnixTimestampToDateTime, the supplied unixTime is still in seconds, right?
                        – Ngoc Pham
                        May 3 at 21:21




                        So in your UnixTimestampToDateTime, the supplied unixTime is still in seconds, right?
                        – Ngoc Pham
                        May 3 at 21:21












                        @NgocPham yes it is
                        – Felix Keil
                        May 4 at 4:56




                        @NgocPham yes it is
                        – Felix Keil
                        May 4 at 4:56










                        up vote
                        12
                        down vote













                        See IdentityModel.EpochTimeExtensions



                        public static class EpochTimeExtensions

                        /// <summary>
                        /// Converts the given date value to epoch time.
                        /// </summary>
                        public static long ToEpochTime(this DateTime dateTime)

                        var date = dateTime.ToUniversalTime();
                        var ticks = date.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks;
                        var ts = ticks / TimeSpan.TicksPerSecond;
                        return ts;


                        /// <summary>
                        /// Converts the given date value to epoch time.
                        /// </summary>
                        public static long ToEpochTime(this DateTimeOffset dateTime)

                        var date = dateTime.ToUniversalTime();
                        var ticks = date.Ticks - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).Ticks;
                        var ts = ticks / TimeSpan.TicksPerSecond;
                        return ts;


                        /// <summary>
                        /// Converts the given epoch time to a <see cref="DateTime"/> with <see cref="DateTimeKind.Utc"/> kind.
                        /// </summary>
                        public static DateTime ToDateTimeFromEpoch(this long intDate)

                        var timeInTicks = intDate * TimeSpan.TicksPerSecond;
                        return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddTicks(timeInTicks);


                        /// <summary>
                        /// Converts the given epoch time to a UTC <see cref="DateTimeOffset"/>.
                        /// </summary>
                        public static DateTimeOffset ToDateTimeOffsetFromEpoch(this long intDate)

                        var timeInTicks = intDate * TimeSpan.TicksPerSecond;
                        return new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddTicks(timeInTicks);







                        share|improve this answer


























                          up vote
                          12
                          down vote













                          See IdentityModel.EpochTimeExtensions



                          public static class EpochTimeExtensions

                          /// <summary>
                          /// Converts the given date value to epoch time.
                          /// </summary>
                          public static long ToEpochTime(this DateTime dateTime)

                          var date = dateTime.ToUniversalTime();
                          var ticks = date.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks;
                          var ts = ticks / TimeSpan.TicksPerSecond;
                          return ts;


                          /// <summary>
                          /// Converts the given date value to epoch time.
                          /// </summary>
                          public static long ToEpochTime(this DateTimeOffset dateTime)

                          var date = dateTime.ToUniversalTime();
                          var ticks = date.Ticks - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).Ticks;
                          var ts = ticks / TimeSpan.TicksPerSecond;
                          return ts;


                          /// <summary>
                          /// Converts the given epoch time to a <see cref="DateTime"/> with <see cref="DateTimeKind.Utc"/> kind.
                          /// </summary>
                          public static DateTime ToDateTimeFromEpoch(this long intDate)

                          var timeInTicks = intDate * TimeSpan.TicksPerSecond;
                          return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddTicks(timeInTicks);


                          /// <summary>
                          /// Converts the given epoch time to a UTC <see cref="DateTimeOffset"/>.
                          /// </summary>
                          public static DateTimeOffset ToDateTimeOffsetFromEpoch(this long intDate)

                          var timeInTicks = intDate * TimeSpan.TicksPerSecond;
                          return new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddTicks(timeInTicks);







                          share|improve this answer
























                            up vote
                            12
                            down vote










                            up vote
                            12
                            down vote









                            See IdentityModel.EpochTimeExtensions



                            public static class EpochTimeExtensions

                            /// <summary>
                            /// Converts the given date value to epoch time.
                            /// </summary>
                            public static long ToEpochTime(this DateTime dateTime)

                            var date = dateTime.ToUniversalTime();
                            var ticks = date.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks;
                            var ts = ticks / TimeSpan.TicksPerSecond;
                            return ts;


                            /// <summary>
                            /// Converts the given date value to epoch time.
                            /// </summary>
                            public static long ToEpochTime(this DateTimeOffset dateTime)

                            var date = dateTime.ToUniversalTime();
                            var ticks = date.Ticks - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).Ticks;
                            var ts = ticks / TimeSpan.TicksPerSecond;
                            return ts;


                            /// <summary>
                            /// Converts the given epoch time to a <see cref="DateTime"/> with <see cref="DateTimeKind.Utc"/> kind.
                            /// </summary>
                            public static DateTime ToDateTimeFromEpoch(this long intDate)

                            var timeInTicks = intDate * TimeSpan.TicksPerSecond;
                            return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddTicks(timeInTicks);


                            /// <summary>
                            /// Converts the given epoch time to a UTC <see cref="DateTimeOffset"/>.
                            /// </summary>
                            public static DateTimeOffset ToDateTimeOffsetFromEpoch(this long intDate)

                            var timeInTicks = intDate * TimeSpan.TicksPerSecond;
                            return new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddTicks(timeInTicks);







                            share|improve this answer














                            See IdentityModel.EpochTimeExtensions



                            public static class EpochTimeExtensions

                            /// <summary>
                            /// Converts the given date value to epoch time.
                            /// </summary>
                            public static long ToEpochTime(this DateTime dateTime)

                            var date = dateTime.ToUniversalTime();
                            var ticks = date.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks;
                            var ts = ticks / TimeSpan.TicksPerSecond;
                            return ts;


                            /// <summary>
                            /// Converts the given date value to epoch time.
                            /// </summary>
                            public static long ToEpochTime(this DateTimeOffset dateTime)

                            var date = dateTime.ToUniversalTime();
                            var ticks = date.Ticks - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).Ticks;
                            var ts = ticks / TimeSpan.TicksPerSecond;
                            return ts;


                            /// <summary>
                            /// Converts the given epoch time to a <see cref="DateTime"/> with <see cref="DateTimeKind.Utc"/> kind.
                            /// </summary>
                            public static DateTime ToDateTimeFromEpoch(this long intDate)

                            var timeInTicks = intDate * TimeSpan.TicksPerSecond;
                            return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddTicks(timeInTicks);


                            /// <summary>
                            /// Converts the given epoch time to a UTC <see cref="DateTimeOffset"/>.
                            /// </summary>
                            public static DateTimeOffset ToDateTimeOffsetFromEpoch(this long intDate)

                            var timeInTicks = intDate * TimeSpan.TicksPerSecond;
                            return new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddTicks(timeInTicks);








                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 14 '16 at 16:34

























                            answered Apr 28 '15 at 1:22









                            orad

                            5,554134982




                            5,554134982




















                                up vote
                                10
                                down vote













                                To supplement ScottCher's answer, I recently found myself in the annoying scenario of having both seconds and milliseconds UNIX timestamps arbitrarily mixed together in an input data set. The following code seems to handle this well:



                                static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                                static readonly double MaxUnixSeconds = (DateTime.MaxValue - UnixEpoch).TotalSeconds;

                                public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)

                                return unixTimeStamp > MaxUnixSeconds
                                ? UnixEpoch.AddMilliseconds(unixTimeStamp)
                                : UnixEpoch.AddSeconds(unixTimeStamp);






                                share|improve this answer
















                                • 1




                                  Be careful when not using the DateTimeKind argument, since the constructed DateTime will be in the computer's local time (thanks for the code, Chris)!
                                  – Sam Grondahl
                                  Sep 16 '13 at 22:40










                                • Beware - this will not work for unix timestamps for dates prior to the 11th of January 1978 if they're represented in milliseconds. A unix datestamp of 253324800 (seconds) gives the correct date of 11.01.1978, whereas the millisecond representation 253324800000 gives a date of 18.07.9997. This may have worked for your data set, but it's not a general solution.
                                  – Øyvind
                                  May 17 at 7:24














                                up vote
                                10
                                down vote













                                To supplement ScottCher's answer, I recently found myself in the annoying scenario of having both seconds and milliseconds UNIX timestamps arbitrarily mixed together in an input data set. The following code seems to handle this well:



                                static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                                static readonly double MaxUnixSeconds = (DateTime.MaxValue - UnixEpoch).TotalSeconds;

                                public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)

                                return unixTimeStamp > MaxUnixSeconds
                                ? UnixEpoch.AddMilliseconds(unixTimeStamp)
                                : UnixEpoch.AddSeconds(unixTimeStamp);






                                share|improve this answer
















                                • 1




                                  Be careful when not using the DateTimeKind argument, since the constructed DateTime will be in the computer's local time (thanks for the code, Chris)!
                                  – Sam Grondahl
                                  Sep 16 '13 at 22:40










                                • Beware - this will not work for unix timestamps for dates prior to the 11th of January 1978 if they're represented in milliseconds. A unix datestamp of 253324800 (seconds) gives the correct date of 11.01.1978, whereas the millisecond representation 253324800000 gives a date of 18.07.9997. This may have worked for your data set, but it's not a general solution.
                                  – Øyvind
                                  May 17 at 7:24












                                up vote
                                10
                                down vote










                                up vote
                                10
                                down vote









                                To supplement ScottCher's answer, I recently found myself in the annoying scenario of having both seconds and milliseconds UNIX timestamps arbitrarily mixed together in an input data set. The following code seems to handle this well:



                                static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                                static readonly double MaxUnixSeconds = (DateTime.MaxValue - UnixEpoch).TotalSeconds;

                                public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)

                                return unixTimeStamp > MaxUnixSeconds
                                ? UnixEpoch.AddMilliseconds(unixTimeStamp)
                                : UnixEpoch.AddSeconds(unixTimeStamp);






                                share|improve this answer












                                To supplement ScottCher's answer, I recently found myself in the annoying scenario of having both seconds and milliseconds UNIX timestamps arbitrarily mixed together in an input data set. The following code seems to handle this well:



                                static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                                static readonly double MaxUnixSeconds = (DateTime.MaxValue - UnixEpoch).TotalSeconds;

                                public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)

                                return unixTimeStamp > MaxUnixSeconds
                                ? UnixEpoch.AddMilliseconds(unixTimeStamp)
                                : UnixEpoch.AddSeconds(unixTimeStamp);







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Oct 7 '12 at 16:14









                                Chris Thoman

                                30133




                                30133







                                • 1




                                  Be careful when not using the DateTimeKind argument, since the constructed DateTime will be in the computer's local time (thanks for the code, Chris)!
                                  – Sam Grondahl
                                  Sep 16 '13 at 22:40










                                • Beware - this will not work for unix timestamps for dates prior to the 11th of January 1978 if they're represented in milliseconds. A unix datestamp of 253324800 (seconds) gives the correct date of 11.01.1978, whereas the millisecond representation 253324800000 gives a date of 18.07.9997. This may have worked for your data set, but it's not a general solution.
                                  – Øyvind
                                  May 17 at 7:24












                                • 1




                                  Be careful when not using the DateTimeKind argument, since the constructed DateTime will be in the computer's local time (thanks for the code, Chris)!
                                  – Sam Grondahl
                                  Sep 16 '13 at 22:40










                                • Beware - this will not work for unix timestamps for dates prior to the 11th of January 1978 if they're represented in milliseconds. A unix datestamp of 253324800 (seconds) gives the correct date of 11.01.1978, whereas the millisecond representation 253324800000 gives a date of 18.07.9997. This may have worked for your data set, but it's not a general solution.
                                  – Øyvind
                                  May 17 at 7:24







                                1




                                1




                                Be careful when not using the DateTimeKind argument, since the constructed DateTime will be in the computer's local time (thanks for the code, Chris)!
                                – Sam Grondahl
                                Sep 16 '13 at 22:40




                                Be careful when not using the DateTimeKind argument, since the constructed DateTime will be in the computer's local time (thanks for the code, Chris)!
                                – Sam Grondahl
                                Sep 16 '13 at 22:40












                                Beware - this will not work for unix timestamps for dates prior to the 11th of January 1978 if they're represented in milliseconds. A unix datestamp of 253324800 (seconds) gives the correct date of 11.01.1978, whereas the millisecond representation 253324800000 gives a date of 18.07.9997. This may have worked for your data set, but it's not a general solution.
                                – Øyvind
                                May 17 at 7:24




                                Beware - this will not work for unix timestamps for dates prior to the 11th of January 1978 if they're represented in milliseconds. A unix datestamp of 253324800 (seconds) gives the correct date of 11.01.1978, whereas the millisecond representation 253324800000 gives a date of 18.07.9997. This may have worked for your data set, but it's not a general solution.
                                – Øyvind
                                May 17 at 7:24










                                up vote
                                6
                                down vote













                                Unix time conversion is new in .NET Framework 4.6.



                                You can now more easily convert date and time values to or from .NET Framework types and Unix time. This can be necessary, for example, when converting time values between a JavaScript client and .NET server. The following APIs have been added to the DateTimeOffset structure:



                                static DateTimeOffset FromUnixTimeSeconds(long seconds)
                                static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)
                                long DateTimeOffset.ToUnixTimeSeconds()
                                long DateTimeOffset.ToUnixTimeMilliseconds()





                                share|improve this answer




















                                • This does not give you local time, you get UTC.
                                  – Berend de Boer
                                  Aug 27 '17 at 23:57










                                • @BerenddeBoer That's a reasonable default. You may apply a custom offset after that as you want.
                                  – Deilan
                                  Nov 21 '17 at 13:04






                                • 1




                                  @BerenddeBoer That misunderstands what unix time is. Unix time is seconds since midnight, Jan 1, 1970, UTC. Doesn't matter where you are, the number of seconds since that epoch doesn't change. Converting it to human readable local time displays are separate from this universal representation, as it should be.
                                  – Tanktalus
                                  Sep 11 at 17:18














                                up vote
                                6
                                down vote













                                Unix time conversion is new in .NET Framework 4.6.



                                You can now more easily convert date and time values to or from .NET Framework types and Unix time. This can be necessary, for example, when converting time values between a JavaScript client and .NET server. The following APIs have been added to the DateTimeOffset structure:



                                static DateTimeOffset FromUnixTimeSeconds(long seconds)
                                static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)
                                long DateTimeOffset.ToUnixTimeSeconds()
                                long DateTimeOffset.ToUnixTimeMilliseconds()





                                share|improve this answer




















                                • This does not give you local time, you get UTC.
                                  – Berend de Boer
                                  Aug 27 '17 at 23:57










                                • @BerenddeBoer That's a reasonable default. You may apply a custom offset after that as you want.
                                  – Deilan
                                  Nov 21 '17 at 13:04






                                • 1




                                  @BerenddeBoer That misunderstands what unix time is. Unix time is seconds since midnight, Jan 1, 1970, UTC. Doesn't matter where you are, the number of seconds since that epoch doesn't change. Converting it to human readable local time displays are separate from this universal representation, as it should be.
                                  – Tanktalus
                                  Sep 11 at 17:18












                                up vote
                                6
                                down vote










                                up vote
                                6
                                down vote









                                Unix time conversion is new in .NET Framework 4.6.



                                You can now more easily convert date and time values to or from .NET Framework types and Unix time. This can be necessary, for example, when converting time values between a JavaScript client and .NET server. The following APIs have been added to the DateTimeOffset structure:



                                static DateTimeOffset FromUnixTimeSeconds(long seconds)
                                static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)
                                long DateTimeOffset.ToUnixTimeSeconds()
                                long DateTimeOffset.ToUnixTimeMilliseconds()





                                share|improve this answer












                                Unix time conversion is new in .NET Framework 4.6.



                                You can now more easily convert date and time values to or from .NET Framework types and Unix time. This can be necessary, for example, when converting time values between a JavaScript client and .NET server. The following APIs have been added to the DateTimeOffset structure:



                                static DateTimeOffset FromUnixTimeSeconds(long seconds)
                                static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)
                                long DateTimeOffset.ToUnixTimeSeconds()
                                long DateTimeOffset.ToUnixTimeMilliseconds()






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jul 23 '15 at 13:09









                                Fred

                                5,35312741




                                5,35312741











                                • This does not give you local time, you get UTC.
                                  – Berend de Boer
                                  Aug 27 '17 at 23:57










                                • @BerenddeBoer That's a reasonable default. You may apply a custom offset after that as you want.
                                  – Deilan
                                  Nov 21 '17 at 13:04






                                • 1




                                  @BerenddeBoer That misunderstands what unix time is. Unix time is seconds since midnight, Jan 1, 1970, UTC. Doesn't matter where you are, the number of seconds since that epoch doesn't change. Converting it to human readable local time displays are separate from this universal representation, as it should be.
                                  – Tanktalus
                                  Sep 11 at 17:18
















                                • This does not give you local time, you get UTC.
                                  – Berend de Boer
                                  Aug 27 '17 at 23:57










                                • @BerenddeBoer That's a reasonable default. You may apply a custom offset after that as you want.
                                  – Deilan
                                  Nov 21 '17 at 13:04






                                • 1




                                  @BerenddeBoer That misunderstands what unix time is. Unix time is seconds since midnight, Jan 1, 1970, UTC. Doesn't matter where you are, the number of seconds since that epoch doesn't change. Converting it to human readable local time displays are separate from this universal representation, as it should be.
                                  – Tanktalus
                                  Sep 11 at 17:18















                                This does not give you local time, you get UTC.
                                – Berend de Boer
                                Aug 27 '17 at 23:57




                                This does not give you local time, you get UTC.
                                – Berend de Boer
                                Aug 27 '17 at 23:57












                                @BerenddeBoer That's a reasonable default. You may apply a custom offset after that as you want.
                                – Deilan
                                Nov 21 '17 at 13:04




                                @BerenddeBoer That's a reasonable default. You may apply a custom offset after that as you want.
                                – Deilan
                                Nov 21 '17 at 13:04




                                1




                                1




                                @BerenddeBoer That misunderstands what unix time is. Unix time is seconds since midnight, Jan 1, 1970, UTC. Doesn't matter where you are, the number of seconds since that epoch doesn't change. Converting it to human readable local time displays are separate from this universal representation, as it should be.
                                – Tanktalus
                                Sep 11 at 17:18




                                @BerenddeBoer That misunderstands what unix time is. Unix time is seconds since midnight, Jan 1, 1970, UTC. Doesn't matter where you are, the number of seconds since that epoch doesn't change. Converting it to human readable local time displays are separate from this universal representation, as it should be.
                                – Tanktalus
                                Sep 11 at 17:18










                                up vote
                                4
                                down vote













                                I found the right answer just by comparing the conversion to 1/1/1970 w/o the local time adjustment;



                                DateTime date = new DateTime(2011, 4, 1, 12, 0, 0, 0);
                                DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                                TimeSpan span = (date - epoch);
                                double unixTime =span.TotalSeconds;





                                share|improve this answer


























                                  up vote
                                  4
                                  down vote













                                  I found the right answer just by comparing the conversion to 1/1/1970 w/o the local time adjustment;



                                  DateTime date = new DateTime(2011, 4, 1, 12, 0, 0, 0);
                                  DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                                  TimeSpan span = (date - epoch);
                                  double unixTime =span.TotalSeconds;





                                  share|improve this answer
























                                    up vote
                                    4
                                    down vote










                                    up vote
                                    4
                                    down vote









                                    I found the right answer just by comparing the conversion to 1/1/1970 w/o the local time adjustment;



                                    DateTime date = new DateTime(2011, 4, 1, 12, 0, 0, 0);
                                    DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                                    TimeSpan span = (date - epoch);
                                    double unixTime =span.TotalSeconds;





                                    share|improve this answer














                                    I found the right answer just by comparing the conversion to 1/1/1970 w/o the local time adjustment;



                                    DateTime date = new DateTime(2011, 4, 1, 12, 0, 0, 0);
                                    DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                                    TimeSpan span = (date - epoch);
                                    double unixTime =span.TotalSeconds;






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jun 23 '13 at 22:16









                                    CyberFox

                                    495418




                                    495418










                                    answered Apr 12 '11 at 20:34









                                    n8CodeGuru

                                    185213




                                    185213




















                                        up vote
                                        3
                                        down vote













                                        DateTime unixEpoch = DateTime.ParseExact("1970-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
                                        DateTime convertedTime = unixEpoch.AddMilliseconds(unixTimeInMillisconds);


                                        Of course, one can make unixEpoch a global static, so it only needs to appear once in your project, and one can use AddSeconds if the UNIX time is in seconds.



                                        To go the other way:



                                        double unixTimeInMilliseconds = timeToConvert.Subtract(unixEpoch).TotalMilliseconds;


                                        Truncate to Int64 and/or use TotalSeconds as needed.






                                        share|improve this answer
























                                          up vote
                                          3
                                          down vote













                                          DateTime unixEpoch = DateTime.ParseExact("1970-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
                                          DateTime convertedTime = unixEpoch.AddMilliseconds(unixTimeInMillisconds);


                                          Of course, one can make unixEpoch a global static, so it only needs to appear once in your project, and one can use AddSeconds if the UNIX time is in seconds.



                                          To go the other way:



                                          double unixTimeInMilliseconds = timeToConvert.Subtract(unixEpoch).TotalMilliseconds;


                                          Truncate to Int64 and/or use TotalSeconds as needed.






                                          share|improve this answer






















                                            up vote
                                            3
                                            down vote










                                            up vote
                                            3
                                            down vote









                                            DateTime unixEpoch = DateTime.ParseExact("1970-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
                                            DateTime convertedTime = unixEpoch.AddMilliseconds(unixTimeInMillisconds);


                                            Of course, one can make unixEpoch a global static, so it only needs to appear once in your project, and one can use AddSeconds if the UNIX time is in seconds.



                                            To go the other way:



                                            double unixTimeInMilliseconds = timeToConvert.Subtract(unixEpoch).TotalMilliseconds;


                                            Truncate to Int64 and/or use TotalSeconds as needed.






                                            share|improve this answer












                                            DateTime unixEpoch = DateTime.ParseExact("1970-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
                                            DateTime convertedTime = unixEpoch.AddMilliseconds(unixTimeInMillisconds);


                                            Of course, one can make unixEpoch a global static, so it only needs to appear once in your project, and one can use AddSeconds if the UNIX time is in seconds.



                                            To go the other way:



                                            double unixTimeInMilliseconds = timeToConvert.Subtract(unixEpoch).TotalMilliseconds;


                                            Truncate to Int64 and/or use TotalSeconds as needed.







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Aug 12 '14 at 17:26









                                            Hot Licks

                                            37.6k1576133




                                            37.6k1576133




















                                                up vote
                                                2
                                                down vote













                                                A Unix tick is 1 second (if I remember well), and a .NET tick is 100 nanoseconds.



                                                If you've been encountering problems with nanoseconds, you might want to try using AddTick(10000000 * value).






                                                share|improve this answer


















                                                • 3




                                                  Unix is seconds past epoch - which is 1/1/70.
                                                  – ScottCher
                                                  Oct 30 '08 at 14:44














                                                up vote
                                                2
                                                down vote













                                                A Unix tick is 1 second (if I remember well), and a .NET tick is 100 nanoseconds.



                                                If you've been encountering problems with nanoseconds, you might want to try using AddTick(10000000 * value).






                                                share|improve this answer


















                                                • 3




                                                  Unix is seconds past epoch - which is 1/1/70.
                                                  – ScottCher
                                                  Oct 30 '08 at 14:44












                                                up vote
                                                2
                                                down vote










                                                up vote
                                                2
                                                down vote









                                                A Unix tick is 1 second (if I remember well), and a .NET tick is 100 nanoseconds.



                                                If you've been encountering problems with nanoseconds, you might want to try using AddTick(10000000 * value).






                                                share|improve this answer














                                                A Unix tick is 1 second (if I remember well), and a .NET tick is 100 nanoseconds.



                                                If you've been encountering problems with nanoseconds, you might want to try using AddTick(10000000 * value).







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Sep 11 '15 at 9:45









                                                Peter Mortensen

                                                13.3k1983111




                                                13.3k1983111










                                                answered Oct 30 '08 at 10:53









                                                Luk

                                                3,70443151




                                                3,70443151







                                                • 3




                                                  Unix is seconds past epoch - which is 1/1/70.
                                                  – ScottCher
                                                  Oct 30 '08 at 14:44












                                                • 3




                                                  Unix is seconds past epoch - which is 1/1/70.
                                                  – ScottCher
                                                  Oct 30 '08 at 14:44







                                                3




                                                3




                                                Unix is seconds past epoch - which is 1/1/70.
                                                – ScottCher
                                                Oct 30 '08 at 14:44




                                                Unix is seconds past epoch - which is 1/1/70.
                                                – ScottCher
                                                Oct 30 '08 at 14:44










                                                up vote
                                                1
                                                down vote













                                                I needed to convert a timeval struct (seconds, microseconds) containing UNIX time to DateTime without losing precision and haven't found an answer here so I thought I just might add mine:



                                                DateTime _epochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                                                private DateTime UnixTimeToDateTime(Timeval unixTime)

                                                return _epochTime.AddTicks(
                                                unixTime.Seconds * TimeSpan.TicksPerSecond +
                                                unixTime.Microseconds * TimeSpan.TicksPerMillisecond/1000);






                                                share|improve this answer
























                                                  up vote
                                                  1
                                                  down vote













                                                  I needed to convert a timeval struct (seconds, microseconds) containing UNIX time to DateTime without losing precision and haven't found an answer here so I thought I just might add mine:



                                                  DateTime _epochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                                                  private DateTime UnixTimeToDateTime(Timeval unixTime)

                                                  return _epochTime.AddTicks(
                                                  unixTime.Seconds * TimeSpan.TicksPerSecond +
                                                  unixTime.Microseconds * TimeSpan.TicksPerMillisecond/1000);






                                                  share|improve this answer






















                                                    up vote
                                                    1
                                                    down vote










                                                    up vote
                                                    1
                                                    down vote









                                                    I needed to convert a timeval struct (seconds, microseconds) containing UNIX time to DateTime without losing precision and haven't found an answer here so I thought I just might add mine:



                                                    DateTime _epochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                                                    private DateTime UnixTimeToDateTime(Timeval unixTime)

                                                    return _epochTime.AddTicks(
                                                    unixTime.Seconds * TimeSpan.TicksPerSecond +
                                                    unixTime.Microseconds * TimeSpan.TicksPerMillisecond/1000);






                                                    share|improve this answer












                                                    I needed to convert a timeval struct (seconds, microseconds) containing UNIX time to DateTime without losing precision and haven't found an answer here so I thought I just might add mine:



                                                    DateTime _epochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                                                    private DateTime UnixTimeToDateTime(Timeval unixTime)

                                                    return _epochTime.AddTicks(
                                                    unixTime.Seconds * TimeSpan.TicksPerSecond +
                                                    unixTime.Microseconds * TimeSpan.TicksPerMillisecond/1000);







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Dec 27 '13 at 6:55









                                                    i3arnon

                                                    78.8k20211254




                                                    78.8k20211254




















                                                        up vote
                                                        0
                                                        down vote













                                                        public static class UnixTime

                                                        private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);

                                                        public static DateTime UnixTimeToDateTime(double unixTimeStamp)

                                                        return Epoch.AddSeconds(unixTimeStamp).ToUniversalTime();




                                                        you can call UnixTime.UnixTimeToDateTime(double datetime))






                                                        share|improve this answer


























                                                          up vote
                                                          0
                                                          down vote













                                                          public static class UnixTime

                                                          private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);

                                                          public static DateTime UnixTimeToDateTime(double unixTimeStamp)

                                                          return Epoch.AddSeconds(unixTimeStamp).ToUniversalTime();




                                                          you can call UnixTime.UnixTimeToDateTime(double datetime))






                                                          share|improve this answer
























                                                            up vote
                                                            0
                                                            down vote










                                                            up vote
                                                            0
                                                            down vote









                                                            public static class UnixTime

                                                            private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);

                                                            public static DateTime UnixTimeToDateTime(double unixTimeStamp)

                                                            return Epoch.AddSeconds(unixTimeStamp).ToUniversalTime();




                                                            you can call UnixTime.UnixTimeToDateTime(double datetime))






                                                            share|improve this answer














                                                            public static class UnixTime

                                                            private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);

                                                            public static DateTime UnixTimeToDateTime(double unixTimeStamp)

                                                            return Epoch.AddSeconds(unixTimeStamp).ToUniversalTime();




                                                            you can call UnixTime.UnixTimeToDateTime(double datetime))







                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Jun 18 at 7:43

























                                                            answered Jun 18 at 7:36









                                                            madan

                                                            284




                                                            284




















                                                                up vote
                                                                0
                                                                down vote













                                                                var dt = DateTime.Now; 
                                                                var unixTime = ((DateTimeOffset)dt).ToUnixTimeSeconds();


                                                                // 1510396991



                                                                var dt = DateTimeOffset.FromUnixTimeSeconds(1510396991);


                                                                // [11.11.2017 10:43:11 +00:00]






                                                                share|improve this answer
























                                                                  up vote
                                                                  0
                                                                  down vote













                                                                  var dt = DateTime.Now; 
                                                                  var unixTime = ((DateTimeOffset)dt).ToUnixTimeSeconds();


                                                                  // 1510396991



                                                                  var dt = DateTimeOffset.FromUnixTimeSeconds(1510396991);


                                                                  // [11.11.2017 10:43:11 +00:00]






                                                                  share|improve this answer






















                                                                    up vote
                                                                    0
                                                                    down vote










                                                                    up vote
                                                                    0
                                                                    down vote









                                                                    var dt = DateTime.Now; 
                                                                    var unixTime = ((DateTimeOffset)dt).ToUnixTimeSeconds();


                                                                    // 1510396991



                                                                    var dt = DateTimeOffset.FromUnixTimeSeconds(1510396991);


                                                                    // [11.11.2017 10:43:11 +00:00]






                                                                    share|improve this answer












                                                                    var dt = DateTime.Now; 
                                                                    var unixTime = ((DateTimeOffset)dt).ToUnixTimeSeconds();


                                                                    // 1510396991



                                                                    var dt = DateTimeOffset.FromUnixTimeSeconds(1510396991);


                                                                    // [11.11.2017 10:43:11 +00:00]







                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Nov 11 at 10:53









                                                                    mesut

                                                                    1,17621326




                                                                    1,17621326




















                                                                        up vote
                                                                        -2
                                                                        down vote













                                                                        For .NET 4.6 and later:



                                                                        public static class UnixDateTime

                                                                        public static DateTimeOffset FromUnixTimeSeconds(long seconds)


                                                                        public static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)

                                                                        if (milliseconds < -62135596800000L

                                                                        public static long ToUnixTimeSeconds(this DateTimeOffset utcDateTime)

                                                                        return utcDateTime.Ticks / 10000000L - 62135596800L;


                                                                        public static long ToUnixTimeMilliseconds(this DateTimeOffset utcDateTime)

                                                                        return utcDateTime.Ticks / 10000L - 62135596800000L;


                                                                        [Test]
                                                                        public void UnixSeconds()

                                                                        DateTime utcNow = DateTime.UtcNow;
                                                                        DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

                                                                        long unixTimestampInSeconds = utcNowOffset.ToUnixTimeSeconds();

                                                                        DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeSeconds(unixTimestampInSeconds);

                                                                        Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
                                                                        Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
                                                                        Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
                                                                        Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
                                                                        Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
                                                                        Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);


                                                                        [Test]
                                                                        public void UnixMilliseconds()

                                                                        DateTime utcNow = DateTime.UtcNow;
                                                                        DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

                                                                        long unixTimestampInMilliseconds = utcNowOffset.ToUnixTimeMilliseconds();

                                                                        DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeMilliseconds(unixTimestampInMilliseconds);

                                                                        Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
                                                                        Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
                                                                        Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
                                                                        Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
                                                                        Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
                                                                        Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);
                                                                        Assert.AreEqual(utcNowOffset.Millisecond, utcNowOffsetTest.Millisecond);







                                                                        share|improve this answer


















                                                                        • 4




                                                                          I do not understand. In .NET 4.6, the BCL already has those methods (see e.g. my comment to the question above, or some of the other new answers (2015). So what should the point be in writing them again? Did you mean that your answer was a solution for versions prior to 4.6?
                                                                          – Jeppe Stig Nielsen
                                                                          Feb 19 '16 at 10:32














                                                                        up vote
                                                                        -2
                                                                        down vote













                                                                        For .NET 4.6 and later:



                                                                        public static class UnixDateTime

                                                                        public static DateTimeOffset FromUnixTimeSeconds(long seconds)


                                                                        public static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)

                                                                        if (milliseconds < -62135596800000L

                                                                        public static long ToUnixTimeSeconds(this DateTimeOffset utcDateTime)

                                                                        return utcDateTime.Ticks / 10000000L - 62135596800L;


                                                                        public static long ToUnixTimeMilliseconds(this DateTimeOffset utcDateTime)

                                                                        return utcDateTime.Ticks / 10000L - 62135596800000L;


                                                                        [Test]
                                                                        public void UnixSeconds()

                                                                        DateTime utcNow = DateTime.UtcNow;
                                                                        DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

                                                                        long unixTimestampInSeconds = utcNowOffset.ToUnixTimeSeconds();

                                                                        DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeSeconds(unixTimestampInSeconds);

                                                                        Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
                                                                        Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
                                                                        Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
                                                                        Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
                                                                        Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
                                                                        Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);


                                                                        [Test]
                                                                        public void UnixMilliseconds()

                                                                        DateTime utcNow = DateTime.UtcNow;
                                                                        DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

                                                                        long unixTimestampInMilliseconds = utcNowOffset.ToUnixTimeMilliseconds();

                                                                        DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeMilliseconds(unixTimestampInMilliseconds);

                                                                        Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
                                                                        Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
                                                                        Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
                                                                        Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
                                                                        Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
                                                                        Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);
                                                                        Assert.AreEqual(utcNowOffset.Millisecond, utcNowOffsetTest.Millisecond);







                                                                        share|improve this answer


















                                                                        • 4




                                                                          I do not understand. In .NET 4.6, the BCL already has those methods (see e.g. my comment to the question above, or some of the other new answers (2015). So what should the point be in writing them again? Did you mean that your answer was a solution for versions prior to 4.6?
                                                                          – Jeppe Stig Nielsen
                                                                          Feb 19 '16 at 10:32












                                                                        up vote
                                                                        -2
                                                                        down vote










                                                                        up vote
                                                                        -2
                                                                        down vote









                                                                        For .NET 4.6 and later:



                                                                        public static class UnixDateTime

                                                                        public static DateTimeOffset FromUnixTimeSeconds(long seconds)


                                                                        public static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)

                                                                        if (milliseconds < -62135596800000L

                                                                        public static long ToUnixTimeSeconds(this DateTimeOffset utcDateTime)

                                                                        return utcDateTime.Ticks / 10000000L - 62135596800L;


                                                                        public static long ToUnixTimeMilliseconds(this DateTimeOffset utcDateTime)

                                                                        return utcDateTime.Ticks / 10000L - 62135596800000L;


                                                                        [Test]
                                                                        public void UnixSeconds()

                                                                        DateTime utcNow = DateTime.UtcNow;
                                                                        DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

                                                                        long unixTimestampInSeconds = utcNowOffset.ToUnixTimeSeconds();

                                                                        DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeSeconds(unixTimestampInSeconds);

                                                                        Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
                                                                        Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
                                                                        Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
                                                                        Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
                                                                        Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
                                                                        Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);


                                                                        [Test]
                                                                        public void UnixMilliseconds()

                                                                        DateTime utcNow = DateTime.UtcNow;
                                                                        DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

                                                                        long unixTimestampInMilliseconds = utcNowOffset.ToUnixTimeMilliseconds();

                                                                        DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeMilliseconds(unixTimestampInMilliseconds);

                                                                        Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
                                                                        Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
                                                                        Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
                                                                        Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
                                                                        Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
                                                                        Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);
                                                                        Assert.AreEqual(utcNowOffset.Millisecond, utcNowOffsetTest.Millisecond);







                                                                        share|improve this answer














                                                                        For .NET 4.6 and later:



                                                                        public static class UnixDateTime

                                                                        public static DateTimeOffset FromUnixTimeSeconds(long seconds)


                                                                        public static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)

                                                                        if (milliseconds < -62135596800000L

                                                                        public static long ToUnixTimeSeconds(this DateTimeOffset utcDateTime)

                                                                        return utcDateTime.Ticks / 10000000L - 62135596800L;


                                                                        public static long ToUnixTimeMilliseconds(this DateTimeOffset utcDateTime)

                                                                        return utcDateTime.Ticks / 10000L - 62135596800000L;


                                                                        [Test]
                                                                        public void UnixSeconds()

                                                                        DateTime utcNow = DateTime.UtcNow;
                                                                        DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

                                                                        long unixTimestampInSeconds = utcNowOffset.ToUnixTimeSeconds();

                                                                        DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeSeconds(unixTimestampInSeconds);

                                                                        Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
                                                                        Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
                                                                        Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
                                                                        Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
                                                                        Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
                                                                        Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);


                                                                        [Test]
                                                                        public void UnixMilliseconds()

                                                                        DateTime utcNow = DateTime.UtcNow;
                                                                        DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

                                                                        long unixTimestampInMilliseconds = utcNowOffset.ToUnixTimeMilliseconds();

                                                                        DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeMilliseconds(unixTimestampInMilliseconds);

                                                                        Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
                                                                        Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
                                                                        Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
                                                                        Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
                                                                        Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
                                                                        Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);
                                                                        Assert.AreEqual(utcNowOffset.Millisecond, utcNowOffsetTest.Millisecond);








                                                                        share|improve this answer














                                                                        share|improve this answer



                                                                        share|improve this answer








                                                                        edited Sep 11 '15 at 12:36









                                                                        Peter Mortensen

                                                                        13.3k1983111




                                                                        13.3k1983111










                                                                        answered May 28 '15 at 23:35









                                                                        superlogical

                                                                        9,27555871




                                                                        9,27555871







                                                                        • 4




                                                                          I do not understand. In .NET 4.6, the BCL already has those methods (see e.g. my comment to the question above, or some of the other new answers (2015). So what should the point be in writing them again? Did you mean that your answer was a solution for versions prior to 4.6?
                                                                          – Jeppe Stig Nielsen
                                                                          Feb 19 '16 at 10:32












                                                                        • 4




                                                                          I do not understand. In .NET 4.6, the BCL already has those methods (see e.g. my comment to the question above, or some of the other new answers (2015). So what should the point be in writing them again? Did you mean that your answer was a solution for versions prior to 4.6?
                                                                          – Jeppe Stig Nielsen
                                                                          Feb 19 '16 at 10:32







                                                                        4




                                                                        4




                                                                        I do not understand. In .NET 4.6, the BCL already has those methods (see e.g. my comment to the question above, or some of the other new answers (2015). So what should the point be in writing them again? Did you mean that your answer was a solution for versions prior to 4.6?
                                                                        – Jeppe Stig Nielsen
                                                                        Feb 19 '16 at 10:32




                                                                        I do not understand. In .NET 4.6, the BCL already has those methods (see e.g. my comment to the question above, or some of the other new answers (2015). So what should the point be in writing them again? Did you mean that your answer was a solution for versions prior to 4.6?
                                                                        – Jeppe Stig Nielsen
                                                                        Feb 19 '16 at 10:32





                                                                        protected by i3arnon Nov 27 '14 at 7:44



                                                                        Thank you for your interest in this question.
                                                                        Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                        Would you like to answer one of these unanswered questions instead?



                                                                        這個網誌中的熱門文章

                                                                        Barbados

                                                                        How to read a connectionString WITH PROVIDER in .NET Core?

                                                                        Node.js Script on GitHub Pages or Amazon S3