Web App 和 Windows 服务之间的 DateTime.ToString 格式不一致 [英] DateTime.ToString format inconsistent between Web App and Windows Service

查看:29
本文介绍了Web App 和 Windows 服务之间的 DateTime.ToString 格式不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试对 DateTime 值执行 ToString() 时,我们遇到了 Web 应用程序和 Windows 服务之间的奇怪行为.请参阅下面的示例.

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

Windows 服务调用的函数与 Web 应用程序调用的函数相同,那么为什么格式不同呢?

The Windows Service calls the same function as the Web Application does, so why is the formatting different then?

推荐答案

问题必须来自不同的文化.使用 DateTime.ToString (String, IFormatProvider) 重载 CultureInfo.InvariantCulture 属性应该可以解决问题:

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);

这篇关于Web App 和 Windows 服务之间的 DateTime.ToString 格式不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆