从 Windows 文件时间转换为日期时间 [英] Convert from a Windows filetime to a DateTime

查看:67
本文介绍了从 Windows 文件时间转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值为 634876488000000000 的时间戳,预计日期是 2012 年 11 月 4 日,18:00:00.当我转换它时,年份是 3612 而不是 2012(日、月和时间都是正确的).

I have a timestamp with the value 634876488000000000 and the date expected is 04th November 2012, 18:00:00. When I convert it though, the year is 3612 instead of 2012 (the day, month and time are all correct).

这是一个例子:

Console.WriteLine(DateTime.FromFileTimeUtc(634876488000000000).ToString());
Console.WriteLine(DateTime.FromFileTime(634876488000000000).ToString());

和输出:

04/11/3612 18:00:00
04/11/3612 18:00:00

04/11/3612 18:00:00
04/11/3612 18:00:00

经过进一步研究,我发现如果我在 Powershell 中使用 Get-Date 634876488000000000,我会得到正确的预期日期 2012 年 11 月 4 日 18:00:00.

Upon further research I discovered that if I use Get-Date 634876488000000000 in Powershell, I get the correct, expected date of 04 November 2012 18:00:00.

谁能解释一下如何使用 C# 将时间戳正确转换为 .NET DateTime 对象?

Could anyone explain how to correctly convert the time stamp to a .NET DateTime object using C# please?

推荐答案

我的第一个猜测是这个数字是 DateTime.Ticks,刚刚在 Visual Studio 中尝试了以下内容,我得到了.{04/11/2012 6:00:00 PM} 用于:

My first guess was that the number is DateTime.Ticks, just tried the following in Visual studio and I get. {04/11/2012 6:00:00 PM} for:

DateTime dt = DateTime.MinValue.AddTicks(634876488000000000);

或者使用 DateTime Constructor,它以刻度为参数.

Or use the DateTime Constructor which takes ticks as parameter.

DateTime dt = new DateTime(634876488000000000);

这篇关于从 Windows 文件时间转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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