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
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)
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);
}
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(); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.