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?

8
  • Possible duplicate of stackoverflow.com/questions/249760/… Commented Feb 3, 2015 at 17:34
  • 2
    So I'd say you need AddSeconds instead? Commented Feb 3, 2015 at 17:35
  • @JoãoMiguelBrandão it isn't a duplicate because the conversion in that article doesn't work Commented Feb 3, 2015 at 17:36
  • @JoãoMiguelBrandão AddSeconds throws an exception, 1415115303410 is in milliseconds Commented Feb 3, 2015 at 17:37
  • Are you sure you calling this method? Commented Feb 3, 2015 at 17:47

8 Answers 8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 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.
Lesson learnt: Be careful if you are in millisecs. or secs. as seen here
unix timestamp is the number of seconds since 1 Jan 1970 00:00 UTC so you may ned to correct to your timezone
9

Just use DateTimeOffset

DateTimeOffset date = DateTimeOffset.FromUnixTimeSeconds(1415115303410)

Comments

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;
 }

2 Comments

How I already said, AddSeconds throws an exception, my number is in milliseconds
is it so difficult to divide by 1000 an turn it into seconds ?
4
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.

2 Comments

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. dotnetfiddle.net/pGF3km
Correct, this is just the same, I thought he had AddSeconds there (got confused from the last edits), still, this is the correct answer...
1

Try This

DateTime date = new DateTime(Convert.ToInt64("1415115303410"));

1 Comment

While this code snippet may solve the question, including an explanation 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!
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.

Comments

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"));

Comments

-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:~ #

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.