DateTime.ToString format inconsistent between Web App and Windows Service

二次信任 提交于 2019-12-11 05:58:19

问题


We are experiencing weird behaviour between a web application and windows service when trying to perform a ToString() on a DateTime value. 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?


回答1:


The problem must come from having different cultures. Using the DateTime.ToString (String, IFormatProvider) overload with the CultureInfo.InvariantCulture property should solve the problem:

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



回答2:


The formatting of dates to strings uses a CultureInfo object to know what format to use.

Each Thread has a Thread.CurrentCulture property.

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.

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

https://dotnetfiddle.net/dsA3VT

Output: en-US

You can set the CultureInfo for the the Thread, or pass it with each call to ToString.

Setting Thread.CultureInfo

You can set the Thread.CultureInfo using the same property as you use to read it.

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

Unfortunately .Net Fiddle doesn't support changing thread properties.

I didn't know this, but bradbury9 pointed out that since .net 4.6 you can set the CultureInfo.CurrentCulture property as well.

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

Unfortunately .Net Fiddle doesn't support changing the culture this way either.

Passing CultureInfo to ToString

'DateTime.ToString' has overloads which can take an IFormatProvider, and CultureInfo impliments IFormatProvider.

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

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



回答3:


it may be what is calling the Windows service is formatting the date. the code certainly is clear enough. Try debugging the windows service by attaching to the running process and see what it generates. If your service consumer is a web app, look at F12 developer tools and see what is getting sent back int he response stream.



来源:https://stackoverflow.com/questions/43065709/datetime-tostring-format-inconsistent-between-web-app-and-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!