[英]How to Convert Timestamp to DateTime?
我想將紀元轉換為人類可讀的日期,反之亦然。
我想寫一些類似於 C# 中的鏈接的東西。
將 Firefox 中 Places.sqlite 文件中的日期轉換為 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 不夠,我嘗試了 long epoch 但沒有用
您的時間是以微秒為單位的 Unix 時間。
如果您使用 .Net 4.6 或更高版本,您可以將其轉換為DateTime
,如下所示。
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)
時間戳是從 1/1/1970 開始的秒數
static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}
這個例子我修復了它
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(); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.