奇怪的纪元日期号码问题 [英] strange epoch date number issue

查看:53
本文介绍了奇怪的纪元日期号码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个数据集,其中有一个日期字段,其日期类似于以下内容:

I'm working with an data set in which there is a date field with dates that look like the following:

42437.4261290278
42437.5460402431
42437.5478825116

42437.4261290278
42437.5460402431
42437.5478825116

,其中较大的数字是最新的.我们中的一个人认为这与unix纪元和备用时间表示有关.我们现在面临的问题是将上述日期改为标准的MM-DD-YYYY格式.有人对如何将这些替代日期格式转换为标准日期有任何想法吗?

with the larger of the numbers being the most recent. One of us believes it is relating to unix epoch and alternate time representations. The issue we are facing now is reading those date's above into standard MM-DD-YYYY format. Any one have any ideas on how to convert these alternate date forms into standard dates?

我正在尝试在C#中执行此操作.作为参考,我希望列出的最后两个日期是2016年3月8日的某个时间,而第一个日期是在此之前的某个时间.

I'm trying to do this in C#. And for reference, I expect that the last two dates listed to be sometime on March 8th, 2016 and the first to be some time before then.

推荐答案

在您断言所代表的日期是2016年3月8日之后,我假设时代开始于1899年12月30日:

Following your assertion that the dates represented are 2016-03-08, I assume the start of the epoch is 1899-12-30:

static string UnixTimeStampToDateAsString(double ts)
{
    DateTime epoch = new DateTime(1899, 12, 30);
    DateTime d = epoch.AddDays(ts);
    return (d.ToString("yyyy-MM-dd HH:mm:ss"));
}

static void Main(string[] args)
{
    foreach (double dateNumber in new double[] { 42437.4261290278, 42437.5460402431, 42437.5478825116 })
    {
        Console.WriteLine(UnixTimeStampToDateAsString(dateNumber));
    }
    Console.ReadLine();

}

输出:

2016-03-08 10:13:37
2016-03-08 13:06:17
2016-03-08 13:08:57

2016-03-08 10:13:37
2016-03-08 13:06:17
2016-03-08 13:08:57

我必须指出,1899年12月30日是一个不太可能的值,但是我想有人可能有使用它的理由".

I have to state that the 30th of December 1899 is a rather unlikely value, but I suppose someone might have had a "reason" to use that.

编辑,感谢@EricLippert,我可以建议这样做:

Edit Thanks to @EricLippert I can suggest this instead:

Console.WriteLine(DateTime.FromOADate(dateNumber).ToString("yyyy-MM-dd HH:mm:ss"));

这篇关于奇怪的纪元日期号码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆