Search code examples
sqlitedatetimemozillaepoch

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?


Solution

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