如何将时间戳转换为日期时间?
How to Convert Timestamp to DateTime?
我想将纪元转换为人类可读的日期,反之亦然。
我想在 C# 中编写类似于 link 的内容。
将 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)
这个例子我修好了
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(); }
时间戳是从 1/1/1970 开始的秒数
static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}
我想将纪元转换为人类可读的日期,反之亦然。
我想在 C# 中编写类似于 link 的内容。
将 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)
这个例子我修好了
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(); }
时间戳是从 1/1/1970 开始的秒数
static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}