开发者

How to convert date from yyyyMMdd format to mm-dd-yyyy format

开发者 https://www.devze.com 2023-02-05 05:50 出处:网络
I want to convert a string w开发者_如何学编程hich contains the date in yyyyMMdd format to mm-dd-yyyy DateTime format.How do I get it?var dateString = \"20050802\";

I want to convert a string w开发者_如何学编程hich contains the date in yyyyMMdd format to mm-dd-yyyy DateTime format. How do I get it?


var dateString = "20050802";
var date = myDate = DateTime.ParseExact(dateString,
                                    "yyyyMMdd",
                                    System.Globalization.CultureInfo.InvariantCulture);


public static DateTime ConvertDate(string date, string pattern)
{
    DateTime retval = DateTime.MinValue;
    if (System.DateTime.TryParseExact(date, pattern, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out retval))
       return retval;

    // Could not convert date..
    return DateTime.MinValue;
}


DateTime myDate = ConvertDate("20101230", "yyyyMMdd");


CultureInfo provider = CultureInfo.CreateSpecificCulture("en-UK");   

string dateString = "19850121"; //in yyyyMMdd format 

string oldFormat = "yyyyMMdd"; 

DateTime result = DateTime.ParseExact(dateString, oldFormat, provider); //MM/dd/yyyyformat 
0

精彩评论

暂无评论...
验证码 换一张
取 消