-2

I have the following values:

1465509600000

1402437600000

I tried the following:

attempt 1:

    public long? CommitmentStartDate { get; set; }

    public long? CommitmentEndDate { get; set; }

    public DateTime? CommitmentStartDateDate => new DateTime( (CommitmentStartDate != null ? (long)CommitmentStartDate: Convert.ToInt64(DateTime.MinValue)) );

    public DateTime? CommitmentEndDateDate => new DateTime(CommitmentEndDate != null ? (long)CommitmentEndDate: Convert.ToInt64(DateTime.MinValue));

This gives me the date in the worng format, i get this:

0001-01-02 16:42:30

0001-01-02 14:57:23

attempt 2:

    static readonly DateTime _unixEpoch =
         new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    public static DateTime DateFromTimestamp(long timestamp)
    {
        return _unixEpoch.AddSeconds(timestamp);
    }

    public long? CommitmentStartDate { get; set; }

    public long? CommitmentEndDate { get; set; }

    public DateTime? CommitmentStartDateDate =>  DateFromTimestamp(CommitmentStartDate != null ? (long)CommitmentStartDate: Convert.ToInt64(DateTime.MinValue));

    public DateTime? CommitmentEndDateDate => DateFromTimestamp(CommitmentEndDate != null ? (long)CommitmentEndDate: Convert.ToInt64(DateTime.MinValue));

this gave me an argumentOutOfRange exception.

How do I do this?

EDIT:

Expected values:

2014-06-11

2016-06-10

EDIT 2:

Ticks that come from date

1402437600000 ---- > 2014-06-11

1465509600000 ---- > 2016-06-10

ThunD3eR
  • 3,216
  • 5
  • 50
  • 94
  • Possible duplicate of [How can I convert ticks to a date format?](http://stackoverflow.com/questions/1489243/how-can-i-convert-ticks-to-a-date-format) – Nope Dec 13 '16 at 12:20
  • Way too much fluff. Please just post 1 or more samples with their expected date values. – H H Dec 13 '16 at 12:20
  • @François Wahl Thats the thread that i used when trying to convert this, would be nice if you would read through the entire question with code before voting to close – ThunD3eR Dec 13 '16 at 12:24
  • `DateTime`s don't have a specific format associated with them. – Daniel A. White Dec 13 '16 at 12:25
  • 1
    could you specify what the "ticks" represent? – nozzleman Dec 13 '16 at 12:26

3 Answers3

2

Your 2 samples are 2 years apart, taking the difference from those ticks and dividing by 2, 365, 24 and 3600 leaves 1000 so they are milliseconds.

A quick check reveals that they are indeed based on 1-1-1970 so

//return _unixEpoch.AddSeconds(timestamp);
return _unixEpoch.AddMilliSeconds(timestamp);
H H
  • 263,252
  • 30
  • 330
  • 514
  • Anyway, with the sample "ticks" OP shows, the results would be `2016-06-09 22:00:00` and `2014-06-10 22:00:00` – Pikoh Dec 13 '16 at 12:34
  • 1
    Yes, there appears to be a Timezone offset involved, I got a base of 1-1-70, 02:00. Easy to compensate but it is always so much better to have specs for your incoming data. – H H Dec 13 '16 at 12:37
1

In this part you are adding seconds:

public static DateTime DateFromTimestamp(long timestamp)
{
    return _unixEpoch.AddSeconds(timestamp);
}

If you want to pass ticks, you need to add ticks:

public static DateTime DateFromTimestamp(long timestamp)
{
    return _unixEpoch.AddTicks(timestamp);
}

But you have to be sure that your ticks mean the same as .NET ticks. One .NET tick is 100 nanoseconds. If your tick is a different unit, you have to convert it to .NET ticks first.

Sefe
  • 13,731
  • 5
  • 42
  • 55
1

I used this extension

public static DateTime FromUnixTicks(this double ms)
{
    DateTime d1 = new DateTime(1970, 1, 1);
    return d1.AddMilliseconds(ms).ToLocalTime();
}

and sample:

1465509600000.0.FromUnixTicks()

and convert back

public static double ToUnixTicks(this DateTime dt)
{
    DateTime d1 = new DateTime(1970, 1, 1);
    DateTime d2 = dt.ToUniversalTime();
    TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
    return ts.TotalMilliseconds;
}

I needed double here, but you can use long, I guess.

tym32167
  • 4,741
  • 2
  • 28
  • 32