如何将任何日期格式转换为yyyy-MM-dd [英] How to convert any date format to yyyy-MM-dd

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

问题描述

我的应用程序解析字符串数据,提取日期并标识日期格式,然后将其转换为yyyy-MM-dd.

来源日期可以是石灰dd-mm-yyyy,dd/mm/yyyy,mm-dd-yyyy,mm/dd/yyyy甚至是yyyy-MM-dd.

除了使用switch case尝试不同的排列组合之外,还有其他有效的方法吗?

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

上面的代码只返回了相同的sourceDate"31-08-2012".

解决方案

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

这些链接也可能对您有帮助

DateTime.ToString()模式

DateTime [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#]

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

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