简体   繁体   English

FileTime到字符串

[英]FileTime to string

I'm reading some Microsoft FileTime values, stored as a long, and I'm trying to convert it into a human readable date. 我正在阅读一些Microsoft FileTime值,存储为long,我正在尝试将其转换为人类可读日期。

For instance, the value 131733712713359180 converts to: Wednesday, June 13, 2018 1:47:51pm . 例如,值131733712713359180将转换为: Wednesday, June 13, 2018 1:47:51pm This was done using the online tool, here: Online time convertor 这是使用在线工具完成的,这里是: 在线时间转换器

I've got it working in Java fine, but when I try to do it in C#, I'm getting the wrong year. 我在Java中工作得很好,但是当我尝试用C#做的时候,我错了一年。 The output I get is: 13/06/0418 13:47:51 . 我得到的输出是: 13/06/0418 13:47:51

The code I'm using to do the conversion is: 我用来进行转换的代码是:

public string CalculateTimestamp(Int64 epoch)
{
    DateTime date = DateTime.Now;

    try
    {
        date = new DateTime(epoch);
        DateTime filetime = new DateTime(date.ToFileTime());
        result = filetime.ToString();
    }
    catch (Exception uhoh)
    {
        result = "failedtoparsetimestamp";
    }

    return result;
}

When doing the conversion in Java, this is the code I'm using. 在Java中进行转换时,这是我正在使用的代码。

public String calculateTimeStamp(long epoch) {

    if (epoch == 0) {
        return "--";
    }

    long unixDifference = 11644473600000L;

    long timeStamp = (epoch / (10 * 1000)) - unixDifference;

    Date date = new Date(timeStamp);

    return date.toString();
}

I guessed that the C# conversion should be more straight forward, but I can't figure out why the year is wrong. 我猜想C#转换应该更直接,但我无法弄清楚为什么今年是错误的。 I've tried both UInt64 and Int64 , both give the same (wrong) result. 我已经尝试了UInt64Int64 ,两者都给出相同(错误)的结果。

Any suggestions would be greatly appreciated. 任何建议将不胜感激。

Thanks 谢谢

This is built-in to DateTime so there's no need to do any adjustments: 这是DateTime内置的,因此无需进行任何调整:

var date = DateTime.FromFileTimeUtc(131733712713359180);

This returns 2018-06-13 13:47:51 这将返回2018-06-13 13:47:51

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM