8

I'm trying to convert this unix timestamp 1415115303410 in DateTime, in this way:

private static DateTime UnixTimeStampToDateTime(long unixTimeStamp)
{
        System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
        dtDateTime = dtDateTime.AddMilliseconds(unixTimeStamp);
        return dtDateTime;
}

But I get a wrong date: Date: {04/11/0045 00:00:00}

NOTE: dtDateTime.AddSeconds(unixTimeStamp) throws an exception.. my number is in Milliseconds.

with this online conversion tool http://www.epochconverter.com/ I get the right conversion:

04/11/2014 15:35:03 GMT+0:00

How I can convert this one?

DevT
  • 1,411
  • 3
  • 16
  • 32
  • Possible duplicate of http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa – João Miguel Brandão Feb 03 '15 at 17:34
  • 2
    So I'd say you need AddSeconds instead? – João Miguel Brandão Feb 03 '15 at 17:35
  • @JoãoMiguelBrandão it isn't a duplicate because the conversion in that article doesn't work – DevT Feb 03 '15 at 17:36
  • @JoãoMiguelBrandão AddSeconds throws an exception, 1415115303410 is in milliseconds – DevT Feb 03 '15 at 17:37
  • Are you sure you calling this method? – Ofiris Feb 03 '15 at 17:47
  • possible duplicate of [How do you convert epoch time in C#?](http://stackoverflow.com/questions/2883576/how-do-you-convert-epoch-time-in-c) – Ofiris Feb 03 '15 at 17:50
  • Possible duplicate of [How to convert a Unix timestamp to DateTime and vice versa?](https://stackoverflow.com/questions/249760/how-to-convert-a-unix-timestamp-to-datetime-and-vice-versa) – BobbyA Dec 13 '17 at 15:29
  • Well then the question is wrong if the number you ant to convert is not a unixtimestamp. Divide by 1000 and then it will be a unixtimestamp you can convert – louigi600 Mar 12 '21 at 22:36

8 Answers8

10

Your code is working just fine, as is. Here is a fiddle.

Everyone that is telling you to use AddSeconds is wrong. The number you are giving us is clearly in milliseconds. There are 31,536,000 seconds in a year. 1415115303410 divided by 31536000 is 4487. There hasn't been 4,487 years passed since 1/1/1970.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • This gives the right date but the wrong time. This date: 1503583200123 is August 25th 2017 at 9AM but your code gives my August 25th 2017 at 2PM – DevShadow Aug 25 '17 at 13:18
  • 1
    @DevShadow This is probably a time zone issue. In my fiddle, I am using UTC time. 2PM is probably the correct time in your time zone. – Icemanind Aug 26 '17 at 15:52
  • **Lesson learnt**: Be careful if you are in millisecs. or secs. as seen [here](https://stackoverflow.com/a/24106694/1705829) – Timo Nov 18 '20 at 15:29
  • unix timestamp is the number of seconds since 1 Jan 1970 00:00 UTC so you may ned to correct to your timezone – louigi600 Mar 12 '21 at 22:40
9

Just use DateTimeOffset

DateTimeOffset date = DateTimeOffset.FromUnixTimeSeconds(1415115303410)

Dmitry
  • 1,095
  • 14
  • 21
8

use AddSeconds instead of AddMilliseconds

 private static DateTime UnixTimeStampToDateTime(long unixTimeStamp) 
 {
    System.DateTime dtDateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
    dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
    return dtDateTime;
 }
Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23
3
public DateTime FromUnixTime(long unixTime)
{
    var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return epoch.AddMilliseconds(unixTime);
}

var date = FromUnixTime(1415115303410); // 11/4/2014 3:35:03 PM

Since your number is in milliseconds, Unix time, use AddMilliseconds.

Community
  • 1
  • 1
Ofiris
  • 6,047
  • 6
  • 35
  • 58
  • I dont see how this is any different from what OP posted. It does work however. I think as @icemanind says, OP's code already works. https://dotnetfiddle.net/pGF3km – crthompson Feb 03 '15 at 17:48
  • Correct, this is just the same, I thought he had `AddSeconds` there (got confused from the last edits), still, this is the correct answer... – Ofiris Feb 03 '15 at 17:49
1

Try This

DateTime date = new DateTime(Convert.ToInt64("1415115303410"));
Muhammed
  • 21
  • 2
  • 2
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – kayess Nov 21 '16 at 08:28
1

Microsoft continue thinking about us! All solutions to add seconds/milliseconds is not working with Visual Studio 2017 (.Net 4.6.1). But there is a new solution:


public static DateTime numStrToDate(String val)
{
    DateTime dRet = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    long dSec;
    if (long.TryParse(val, out dSec))
    {
        TimeSpan ts = new TimeSpan(dSec*10l);
        dRet = dRet.Add(ts);
    }
    return dRet;
}

If you need a UTC time - just add 'System.DateTimeKind.Utc' to the DateTime constructor call.

0

Date to Timestamp

 DateTime date = DateTime.ParseExact(date_string, "dd/MM/yyyy H:mm:ss", null);
 Double timestamp = Math.Truncate((date.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalSeconds);

Timestamp to Date

 DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Unspecified);
 dtDateTime = dtDateTime.AddSeconds(Double.Parse(arrayFinalResponse[i, 5])).ToLocalTime();
 String date = dtDateTime.ToString("dd/MM/yyyy H:mm:ss", CultureInfo.GetCultureInfo("en-US"));
-1

You can do the conversion by using a little trick with date command. It does depend on your timezone. I live in UTC + 1 so for me it is like this:

h1x1binax:~ # date -d "Thu Jan  1 01:00:00 CET 1970 + 1415115303410 second"
Thu Mar 21 09:16:50 CET 46813
h1x1binax:~ #

So that's not a unixtime timestamp ... it is probably in milliseconds so need to divide by 1000

h1x1binax:~ # date -d "Thu Jan  1 01:00:00 CET 1970 + 1415115303 second"
Tue Nov  4 16:35:03 CET 2014
h1x1binax:~ #
louigi600
  • 716
  • 6
  • 16