Suppose I have one datestring input ie "30.03.2016" (ie assuming here it may be any lanuage format) but my current culture is United States. I want to convert the input to Windows saved locale setting culture. I am using below format for conversion,but gives me invalid datetime.
var myDateTimeValue = "30.03.2015";
DateTime myDateTime = DateTime.Parse(myDateTimeValue);
var dateingddmmyy = myDateTime.ToString(new CultureInfo("sv-SE"));
Console.Write(dateingddmmyy);
Please let me know how can I convert any type of date string to current culture format.
The source date could be anything like:
I suggest you take a step back and explore what you are trying to solve in more details. Maybe you don't need to convert from any data format, maybe there is a way to provide such.
Below i have solved my issue :
string[] formats = {"M/d/yyyy", "MM/dd/yyyy",
"d/M/yyyy", "dd/MM/yyyy",
"yyyy/M/d", "yyyy/MM/dd",
"M-d-yyyy", "MM-dd-yyyy",
"d-M-yyyy", "dd-MM-yyyy",
"yyyy-M-d", "yyyy-MM-dd",
"M.d.yyyy", "MM.dd.yyyy",
"d.M.yyyy", "dd.MM.yyyy",
"yyyy.M.d", "yyyy.MM.dd",
"M,d,yyyy", "MM,dd,yyyy",
"d,M,yyyy", "dd,MM,yyyy",
"yyyy,M,d", "yyyy,MM,dd",
"M d yyyy", "MM dd yyyy",
"d M yyyy", "dd MM yyyy",
"yyyy M d", "yyyy MM dd"
};
DateTime dateValue;
foreach (string dateStringFormat in formats)
{
if (DateTime.TryParseExact(strDateTime, dateStringFormat,CultureInfo.CurrentCulture,DateTimeStyles.None,
out dateValue))
{
return dateValue.ToShortDateString();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.