-1

I want to convert epoch to human readable date and vice versa.
I want to write something similar to link in C#.

Converting the date in the places.sqlite file in Firefox to a DateTime.

static void Main(string[] args)
{
    //1540787809621000
    string epoch = "1540787809621000";
}

private string epoch2string(int epoch) {
    return 
        new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
            .AddSeconds(epoch)
            .ToShortDateString(); 
}

int size Not enough I try long epoch but not work

Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
Thanawat test
  • 76
  • 1
  • 1
  • 9

1 Answers1

13

Your time is a Unix time in microseconds.

If you are using .Net 4.6 or later, you can convert this to a DateTime as follows.

long time = 1540787809621000; // Unix time in microseconds.

time /= 1000; // Divide by 1,000 because we need milliseconds, not microseconds.

DateTime result = DateTimeOffset.FromUnixTimeMilliseconds(time).DateTime;

Console.WriteLine(result); // Prints 29/10/2018 04:36:49 (UK format)
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276