我想在我的应用程序中获得有效的时间戳,所以我写道:

public static String GetTimestamp(DateTime value)
{
    return value.ToString("yyyyMMddHHmmssffff");
}
//  ...later on in the code
String timeStamp = GetTimestamp(new DateTime());
Console.WriteLine(timeStamp);

输出:
000101010000000000

我想要这样的东西:
20140112180244

我做错了什么?

最佳答案

您的错误是使用 new DateTime() ,它返回 0001 年 1 月 1 日 00:00:00.000 而不是当前日期和时间。获取当前日期和时间的正确语法是 DateTime.Now ,所以改变这个:

String timeStamp = GetTimestamp(new DateTime());

对此:
String timeStamp = GetTimestamp(DateTime.Now);

关于c# - 如何在C#中获得正确的时间戳,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21219797/

10-14 16:44
查看更多