michelle
michelle

Reputation: 2807

Convert DateTime Object in C# to an epoch in Mozilla

I need to be able to convert a DateTime object in C# to an epoch like the one stored in places.sqlite.

I've tried doing it this way, but I've realised it gives me a date in the future!

public static long convertDateTimeToEpoch(DateTime time)
{
    DateTime epoch = new DateTime(1970, 1, 1);

    TimeSpan ts = time - epoch;
    return (long) ts.Ticks/ 10;
}

What am I doing wrong? Can someone please tell me the proper way of converting as I have not found any examples?

Upvotes: 0

Views: 2684

Answers (1)

Dimitri
Dimitri

Reputation: 7013

I think what you're looking for is this:

public static long convertDateTimeToEpoch(DateTime time)
{
    DateTime epoch = new DateTime(1970, 1, 1);

    return time.Subtract(epoch).TotalMilliseconds;
}

Upvotes: 4

Related Questions