How to convert date from yyyyMMdd format to mm-dd-yyyy format
我想将包含
1 2 3 4 | var dateString ="20050802"; var date = myDate = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture); |
1 2 3 4 5 6 7 8 9 10 11 12 | 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"); |
1 2 3 4 5 6 7 | 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 |