C# 如何将格式为“dd/MM/yyyy”的日期字符串转换为操作系统当前文化日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12722734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:13:12  来源:igfitidea点击:

How to Convert a Date String with Format "dd/MM/yyyy" to OS Current Culture Date Format

c#winformsvisual-studio-2008datetime

提问by Itz.Irshad

A string has the value in "dd/MM/yyyy" format like "04/10/2012". This should be converted to a Date w.r.t Current Cultureof OS.

字符串具有“ dd/MM/yyyy”格式的值,例如“ 04/10/2012”。这应该转换为 Date wrt Current Cultureof OS

I have tried below string with Korean as Current Cultureof OS in which date format is yyyy-MM-dd, my code is not getting correct Month value, it interchange the month value with day:

我已经尝试过将韩语作为当前操作系统文化的字符串,其中日期格式为yyyy-MM-dd,我的代码没有得到正确的 Month 值,它将月值与日互换:

Input: "04/10/2012" Output: 2012-04-10

输入:“ 04/10/2012” 输出:2012-04-10

Code:

代码:

DateTime DT;
string dt = "04/10/2012";

DateTimeFormatInfo DateInfo = CultureInfo.CurrentCulture.DateTimeFormat;
DT = Convert.ToDateTime(String.Format ("{0:"+DateInfo .ShortDatePattern +"}", dt.Trim ()), CultureInfo .CurrentCulture);
MessageBox.Show("Date: " + DT.ToShortDateString());

How I can fix that ?

我该如何解决?

采纳答案by Adam Houldsworth

It looks to me like you need to parse it with a fixed format, I think you are currently parsing it with a format other than "dd/MM/yyyy" and because the date is ambiguous (as in, month and day can be interchanged without causing invalid dates) the format is simply switching the month and day value. When you then go to output it, it looks reversed.

在我看来,您需要使用固定格式解析它,我认为您目前正在使用“dd/MM/yyyy”以外的格式解析它,并且因为日期不明确(例如,月和日可以互换)不会导致无效日期)格式只是切换月和日值。当你再去输出它时,它看起来是相反的。

Use DateTime.ParseExactto force the parsing format and then use the built-in current culture sensitive string output methods on DateTimeto get a formatted string:

使用DateTime.ParseExact强制解析格式,然后使用内置在当前文化敏感字符串输出方法DateTime得到的格式化字符串:

var date = DateTime.ParseExact("04/10/2012", "dd/MM/yyyy", CultureInfo.InvariantCulture);

MessageBox.Show("Date: " + date.ToShortDateString()); // Assumes current culture is controlling format

回答by Matthew Watson

Since your input string is in a fixed format, you should parse it in that format:

由于您的输入字符串采用固定格式,因此您应该以该格式解析它:

DateTime.ParseExact(dt, "dd/MM/yyyy", CultureInfo.InvariantCulture);

回答by ChrisF

If you string has the format dd/MM/yyyythen you haveto use DateTime.ParseExactwith the specified format:

如果您的字符串具有格式,dd/MM/yyyy那么您必须使用DateTime.ParseExact指定的格式:

DateTime.ParseExact(dt, "dd/MM/yyyy", CultureInfo.InvariantCulture);

Anything else will try an interpret the string according to the current culture's rules - which, as you have found, will fail.

其他任何东西都会尝试根据当前文化的规则来解释字符串 - 正如您发现的那样,这将失败。

回答by Radek82

why not use ToShortDateTimeString()

为什么不使用 ToShortDateTimeString()