C# 如何将时间戳转换为日期时间?
我想将历元转换为人类可读的日期,反之亦然。C# 如何将时间戳转换为日期时间?,c#,firefox,timestamp,epoch,C#,Firefox,Timestamp,Epoch,我想将历元转换为人类可读的日期,反之亦然。 我想写一些类似于C#的东西 将Firefox中places.sqlite文件中的日期转换为DateTime static void Main(string[] args) { //1540787809621000 string epoch = "1540787809621000"; } private string epoch2string(int epoch) { return new DateTime(1
我想写一些类似于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大小不够,我尝试了较长的历元,但没有工作您的时间是以微秒为单位的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(); }
时间戳是从1970年1月1日开始的秒数
static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}
请分享您的c#代码以及“不工作”的含义。当您必须在问题中添加信息时,请使用帖子底部的按钮,可能重复@ThanawutPadermwong。答案框是用来回答问题的,不是用来澄清问题的。谢谢你老兄弟时间/1000因为毫秒你是家人吗?谁知道你的名字@Thanawutpadermwong谁知道你的名字。对不起,我不明白。请编辑您的问题,而不是发布更新作为答案。我现在已经为你做了这件事。