C# - 如何将任何日期格式转换为YYYY-MM-DD [英] c# - how to convert any date format to yyyy-MM-dd

查看:1496
本文介绍了C# - 如何将任何日期格式转换为YYYY-MM-DD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序解析字符串数据,提取日期及日期的格式,并将其转换为YYYY-MM-DD。



来源日期可能是什么石灰DD-MM-YYYY,DD / MM / YYYY,MM-DD-YYYY,MM / DD / YYYY甚至YYYY-MM-DD。



除尝试使用开关情况不同的排列组合,有没有其他有效的方式来做到这一点?

 字符串sourceDate =31-08 -2012; 
的String.Format({0:YYYY-MM-DD},sourceDate);



上面的代码只是返回相同sourceDate31-08-2012。


解决方案

 字符串DateString =2009/11/12; 
的IFormatProvider文化=新的CultureInfo(EN-US,真正的);
的DateTime dateVal = DateTime.ParseExact(DateString,YYYY-MM-DD,文化);



这些链接还可以帮助你。



则DateTime.ToString()

$模式b
$ b

字符串格式的日期时间[C#]


My app parses a string data, extracts the date and identify the format of the date and convert it to yyyy-MM-dd.

The source date could be anything lime dd-mm-yyyy, dd/mm/yyyy, mm-dd-yyyy, mm/dd/yyyy or even yyyy-MM-dd.

Other than attempting different permutations and combinations using switch case, is there any other efficient way to do it?

string sourceDate = "31-08-2012";
String.Format("{0:yyyy-MM-dd}", sourceDate);

The above code simply returns the same sourceDate "31-08-2012".

解决方案

string DateString = "11/12/2009";
IFormatProvider culture = new CultureInfo("en-US", true); 
DateTime dateVal = DateTime.ParseExact(DateString, "yyyy-MM-dd", culture);

These Links might also Help you

DateTime.ToString() Patterns

String Format for DateTime [C#]

这篇关于C# - 如何将任何日期格式转换为YYYY-MM-DD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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