简体   繁体   English

Web App和Windows Service之间的DateTime.ToString格式不一致

[英]DateTime.ToString format inconsistent between Web App and Windows Service

We are experiencing weird behaviour between a web application and windows service when trying to perform a ToString() on a DateTime value. 尝试对DateTime值执行ToString()时,我们在Web应用程序和Windows服务之间遇到奇怪的行为。 See the example below. 请参见下面的示例。

DateTime parsedReportDate;
reportDate = DateTime.Now.ToString("yyyyMMdd");
reportDateWithSlash = DateTime.Now.ToString("dd/MM/yyyy");
if (DateTime.TryParse(MyDateValue, out parsedReportDate))
{    
    reportDate = parsedReportDate.ToString("yyyyMMdd");
    reportDateWithSlash = parsedReportDate.ToString("dd/MM/yyyy");
}

--reportDateWithSlash on Web Application: 28/03/2017
--reportDateWithSlash on Windows Service: 28-03-2017

The Windows Service calls the same function as the Web Application does, so why is the formatting different then? Windows服务调用与Web应用程序相同的功能,那么为什么格式不同呢?

The formatting of dates to strings uses a CultureInfo object to know what format to use. 将日期格式化为字符串会使用CultureInfo对象来了解要使用的格式。

Each Thread has a Thread.CurrentCulture property. 每个Thread都有一个Thread.CurrentCulture属性。

You can find out what CultureInfo the current Thread is set by getting the current Thread using Thread.CurrentThread and then inspecting it's Thread.CurrentCulture property. 您可以通过使用Thread.CurrentThread获取当前Thread ,然后检查它的Thread.CurrentCulture属性来找出当前Thread设置了什么CultureInfo

public class Program
{
    public static void Main()
    {
        Console.WriteLine(Thread.CurrentThread.CurrentCulture.Name);
    }
}

https://dotnetfiddle.net/dsA3VT https://dotnetfiddle.net/dsA3VT

Output: en-US 输出: en-US

You can set the CultureInfo for the the Thread , or pass it with each call to ToString . 您可以为Thread设置CultureInfo ,或在每次调用ToString将其传递。

Setting Thread.CultureInfo 设置Thread.CultureInfo

You can set the Thread.CultureInfo using the same property as you use to read it. 您可以使用与读取它相同的属性来设置Thread.CultureInfo

Thread.CurrentCulture = new CultureInfo("en-gb");

Unfortunately .Net Fiddle doesn't support changing thread properties. 不幸的是,.Net Fiddle不支持更改线程属性。

I didn't know this, but bradbury9 pointed out that since .net 4.6 you can set the CultureInfo.CurrentCulture property as well. 我不知道这一点,但是bradbury9指出,从.net 4.6开始,您还可以设置CultureInfo.CurrentCulture属性。

CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL");

Unfortunately .Net Fiddle doesn't support changing the culture this way either. 不幸的是,.Net Fiddle也不支持以这种方式改变文化。

Passing CultureInfo to ToString 将CultureInfo传递给ToString

'DateTime.ToString' has overloads which can take an IFormatProvider , and CultureInfo impliments IFormatProvider . “ DateTime.ToString”具有重载,该重载可以带有IFormatProvider ,而CultureInfo暗含IFormatProvider

DateTime.Now.ToString(new CultureInfo("en-gb"));

https://dotnetfiddle.net/qkS5HF https://dotnetfiddle.net/qkS5HF

public class Program
{
    public static void Main()
    {
        var dateTime = DateTime.Now;

        Console.WriteLine(Thread.CurrentThread.CurrentCulture.Name);
        Console.WriteLine(dateTime.ToString(CultureInfo.InvariantCulture));     
        Console.WriteLine(dateTime.ToString(new CultureInfo("en-us")));
    }
}

Output: 输出:

en-US
03/28/2017 09:43:49
3/28/2017 9:43:49 AM

The problem must come from having different cultures. 问题必须来自不同的文化。 Using the DateTime.ToString (String, IFormatProvider) overload with the CultureInfo.InvariantCulture property should solve the problem: CultureInfo.InvariantCulture属性中使用DateTime.ToString(String,IFormatProvider)重载应该可以解决此问题:

DateTime.Now.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

it may be what is calling the Windows service is formatting the date. 可能是Windows服务正在格式化日期。 the code certainly is clear enough. 代码肯定很清楚。 Try debugging the windows service by attaching to the running process and see what it generates. 尝试通过附加到正在运行的进程来调试Windows服务,并查看其生成的内容。 If your service consumer is a web app, look at F12 developer tools and see what is getting sent back int he response stream. 如果您的服务使用者是一个Web应用程序,请查看F12开发人员工具,然后查看响应流中返回的内容。

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

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