【问题标题】:ASP.NET Server-side datetime problemASP.NET 服务器端日期时间问题
【发布时间】:2011-10-24 02:54:49
【问题描述】:

我遇到了一个不知道如何解决的问题。我开发了一个使用 DateTime.UtcNow 的 Web 应用程序。在我的本地机器上测试并且一切正常后,我将应用程序部署到服务器。服务器正在翻转月份和日期,因此它不是 8 月 10 日,而是说是 10 月 8 日。这一定是区域性的,但我不想强制使用特定格式。最好的解决方案是什么?

编辑:我认为这可能是因为我将日期时间转换为字符串。我应该如何根据机器将它们表示为字符串?

【问题讨论】:

  • 所以它的输出像 8/10 而不是 10/8?我认为在某些地区这是预期的格式。
  • 抱歉,如果您不想强制使用特定格式,那么问题是什么。
  • 问题是我需要一个在所有地区都有效的字符串。

标签: asp.net datetime


【解决方案1】:

当显示 DateTime 时,使用特定的文化对其进行格式化。

myDateTime.ToString(CultureInfo.InvariantCulture);
myDateTime.ToString(new CultureInfo("en-GB"));
myDateTime.ToString(new CultureInfo("en-US"));

我还建议阅读 standardcustom 日期和时间格式字符串。

【讨论】:

  • CultureInfo.InvariantCulture:那么这会给所有地区提供正确的时间吗?
  • @Jon - 它代表“无文化”,基于en-US。所以,在英国是不正确的。
  • 谢谢,有道理。那么如果用户将美国时间传递给位于加拿大的服务器会发生什么?
  • @Jon - 如果您使用InvariantCulture 格式化DateTime,它将以相同的方式显示任何地方
【解决方案2】:

您可以使用 ISO-Fromat 读取和保存日期

请看下面:

Convert dateTime to ISO format yyyy-mm-dd hh:mm:ss in C#

【讨论】:

    【解决方案3】:

    MSDN 有一个很好的例子:

    using System;
    
    public class DateToStringExample
    {
       public static void Main()
       {
          DateTime dateValue = new DateTime(2008, 6, 15, 21, 15, 07);
          // Create an array of standard format strings.
          string[] standardFmts = {"d", "D", "f", "F", "g", "G", "m", "o", 
                                   "R", "s", "t", "T", "u", "U", "y"};
          // Output date and time using each standard format string.
          foreach (string standardFmt in standardFmts)
             Console.WriteLine("{0}: {1}", standardFmt, 
                               dateValue.ToString(standardFmt));
          Console.WriteLine();
    
          // Create an array of some custom format strings.
          string[] customFmts = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f", 
                                 "dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" };
          // Output date and time using each custom format string.
          foreach (string customFmt in customFmts)
             Console.WriteLine("'{0}': {1}", customFmt,
                               dateValue.ToString(customFmt));
       }
    }
    // This example displays the following output to the console:
    //       d: 6/15/2008
    //       D: Sunday, June 15, 2008
    //       f: Sunday, June 15, 2008 9:15 PM
    //       F: Sunday, June 15, 2008 9:15:07 PM
    //       g: 6/15/2008 9:15 PM
    //       G: 6/15/2008 9:15:07 PM
    //       m: June 15
    //       o: 2008-06-15T21:15:07.0000000
    //       R: Sun, 15 Jun 2008 21:15:07 GMT
    //       s: 2008-06-15T21:15:07
    //       t: 9:15 PM
    //       T: 9:15:07 PM
    //       u: 2008-06-15 21:15:07Z
    //       U: Monday, June 16, 2008 4:15:07 AM
    //       y: June, 2008
    //       
    //       'h:mm:ss.ff t': 9:15:07.00 P
    //       'd MMM yyyy': 15 Jun 2008
    //       'HH:mm:ss.f': 21:15:07.0
    //       'dd MMM HH:mm:ss': 15 Jun 21:15:07
    //       '\Mon\t\h\: M': Month: 6
    //       'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00
    

    您可以阅读更多关于它的信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多