【问题标题】:Why would I get year 2555 when formatting DateTime with current time?为什么用当前时间格式化 DateTime 时会得到 2555 年?
【发布时间】:2012-10-18 13:31:56
【问题描述】:

我有执行以下操作的 C# 代码:

 DateTime now = DateTime.UtcNow;
 string timeToOutput = now.ToString();
 String pathInStorage = now.ToString("yyyy-MM-dd/HH/");

 CloudBlobClient client = getClient();
 CloudBlobContainer container = client.GetContainerReference(hardcodedContainerName);
 container.GetBlobReference(pathInStorage).UploadText(timeToOutput);

如您所见,我首先获取当前时间,然后使用不同的格式字符串对其进行两次格式化。现在大多数情况下,存储中的路径将匹配到 blob 本身的时间输出。

但有时(很少)blob 中的时间是 2012 年(如 2012 年 10 月 29 日 12:33:00),但路径将包含 2555 年,小时-日-月将与内部时间相匹配blob(如 2555-10-29-12)。

怎么会这样?

【问题讨论】:

  • now 变量的确切值是多少?

标签: c# .net datetime azure azure-storage


【解决方案1】:

Thai Solar Calendar 中的 2555 年是 2012 年。不知道你为什么会得到这个,也许执行代码的服务器有泰国语言环境?

要使代码按预期运行,请使用不同的、可识别区域设置的 ToString() 版本。见DateTime.ToString()

【讨论】:

    【解决方案2】:

    你能通过不变文化做到这一点吗?

    类似:

    DateTime now = DateTime.UtcNow;
    string timeToOutput = now.ToString();
    CultureInfo InvC = new CultureInfo("");
    String pathInStorage = now.ToString("yyyy-MM-dd/HH/", InvC);
    //rest of your logic
    

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 2011-12-06
      • 1970-01-01
      • 2013-01-16
      • 2016-01-05
      • 1970-01-01
      • 2013-06-12
      • 2012-09-27
      相关资源
      最近更新 更多