-2

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,554
  • 3
  • 22
  • 35

3 Answers3

5

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
  • 96,889
  • 9
  • 144
  • 240
2

timestamp is amount of seconds from 1/1/1970

static DateTime ConvertFromUnixTimestamp(double timestamp)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return origin.AddSeconds(timestamp);
}
Divyesh_08
  • 1,563
  • 15
  • 25
-1

This example I fix it

    static void Main(string[] args)
    {
        //1540787809621000
        int epoch = "1540787809621000"; //this microseconds 
        epoch /= 1000; // convert to milliseconds by divide 1000  
        epoch2string(epoch) 
    }

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