.NET DateTime.ToString() -默认格式设置

时间:2021-07-17 02:26:53

Is the default format of ToString dependent on anything server related? Here's the issue: I'm testing and have tested an application on my local machine and ToString(), by default, is returning in a format of "MM/dd/yyyy hh:mm:ss tt", however on our server it appears to be returning as "dd/MM/yyyy hh:mm:ss tt" which the consuming application is not expecting and causing errors.

ToString的默认格式是否依赖于任何与服务器相关的内容?这里的问题是:我在本地机器上测试并测试了一个应用程序,ToString()默认返回的格式是“MM/dd/ yyyyyyyyyyyy hh: MM:ss tt”,但是在我们的服务器上,它返回的格式似乎是“dd/MM/ yyyyyyyy hh: MM:ss tt”,这是消费应用程序并不期望的格式,并导致错误。

Dim uvExpireDate = DateTime.Now.AddMinutes(1)
Dim token = String.Format(fmtString, uvExpireDate.ToUniversalTime().ToString(), [various other params])

Thanks in advance for your help.

谢谢你的帮助。

3 个解决方案

#1


5  

The formatting depends on the default Culture defined on the server.

格式取决于服务器上定义的默认文化。

If you want a specific Culture to apply, you need to use an overload that takes an IFormatProvider, or set the current thread Culture and UICulture to the wanted Culture.

如果您想要应用特定的文化,您需要使用一个包含IFormatProvider的重载,或者将当前的线程文化和UICulture设置为需要的区域性。

InvariantCulture is a Culture that represents no specific culture but is based on en-US, so may be suitable for your use:

不变文化是一种文化,它不代表特定的文化,而是基于en-US,所以可能适合你的使用:

uvExpireDate.ToUniversalTime().ToString(CultureInfo.InvariantCulture)

So, the whole line would be:

所以,整条线是

Dim token = String.Format(fmtString, _ 
            uvExpireDate.ToUniversalTime().ToString(CultureInfo.InvariantCulture), _ 
            [various other params])

#2


0  

The computer "Region and Language Options" (Control Panel) specify the Date Format.

计算机“区域和语言选项”(控制面板)指定日期格式。

You can hardcode the date format: For Example:

您可以硬编码日期格式:例如:

uvExpireData.ToString(@"yyyyMMdd HH.mm.ss")

#3


0  

MSDN shows how to use code to set the culture if you can't change it on the server (Law of Unintended Consequences might apply):

MSDN展示了如果不能在服务器上更改的话,如何使用代码来设置区域性(意外后果定律可能适用):

using System;
using System.Globalization;
using System.Threading;

public class FormatDate
{
   public static void Main()
   {
      DateTime dt = DateTime.Now;
      // Sets the CurrentCulture property to U.S. English.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      // Displays dt, formatted using the ShortDatePattern
      // and the CurrentThread.CurrentCulture.
      Console.WriteLine(dt.ToString("d"));

      // Creates a CultureInfo for German in Germany.
      CultureInfo ci = new CultureInfo("de-DE");
      // Displays dt, formatted using the ShortDatePattern
      // and the CultureInfo.
      Console.WriteLine(dt.ToString("d", ci));
   }
}

#1


5  

The formatting depends on the default Culture defined on the server.

格式取决于服务器上定义的默认文化。

If you want a specific Culture to apply, you need to use an overload that takes an IFormatProvider, or set the current thread Culture and UICulture to the wanted Culture.

如果您想要应用特定的文化,您需要使用一个包含IFormatProvider的重载,或者将当前的线程文化和UICulture设置为需要的区域性。

InvariantCulture is a Culture that represents no specific culture but is based on en-US, so may be suitable for your use:

不变文化是一种文化,它不代表特定的文化,而是基于en-US,所以可能适合你的使用:

uvExpireDate.ToUniversalTime().ToString(CultureInfo.InvariantCulture)

So, the whole line would be:

所以,整条线是

Dim token = String.Format(fmtString, _ 
            uvExpireDate.ToUniversalTime().ToString(CultureInfo.InvariantCulture), _ 
            [various other params])

#2


0  

The computer "Region and Language Options" (Control Panel) specify the Date Format.

计算机“区域和语言选项”(控制面板)指定日期格式。

You can hardcode the date format: For Example:

您可以硬编码日期格式:例如:

uvExpireData.ToString(@"yyyyMMdd HH.mm.ss")

#3


0  

MSDN shows how to use code to set the culture if you can't change it on the server (Law of Unintended Consequences might apply):

MSDN展示了如果不能在服务器上更改的话,如何使用代码来设置区域性(意外后果定律可能适用):

using System;
using System.Globalization;
using System.Threading;

public class FormatDate
{
   public static void Main()
   {
      DateTime dt = DateTime.Now;
      // Sets the CurrentCulture property to U.S. English.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      // Displays dt, formatted using the ShortDatePattern
      // and the CurrentThread.CurrentCulture.
      Console.WriteLine(dt.ToString("d"));

      // Creates a CultureInfo for German in Germany.
      CultureInfo ci = new CultureInfo("de-DE");
      // Displays dt, formatted using the ShortDatePattern
      // and the CultureInfo.
      Console.WriteLine(dt.ToString("d", ci));
   }
}