public static DateTime ConvertDateTime(string Date)
{
DateTime date=new DateTime();
try
{
string CurrentPattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
string[] Split = new string[] {"-","/",@"\","."};
string[] Patternvalue = CurrentPattern.Split(Split,StringSplitOptions.None);
string[] DateSplit = Date.Split(Split,StringSplitOptions.None);
string NewDate = "";
if (Patternvalue[0].ToLower().Contains("d") == true && Patternvalue[1].ToLower().Contains("m")==true && Patternvalue[2].ToLower().Contains("y")==true)
{
NewDate = DateSplit[1] + "/" + DateSplit[0] + "/" + DateSplit[2];
}
else if (Patternvalue[0].ToLower().Contains("m") == true && Patternvalue[1].ToLower().Contains("d")==true && Patternvalue[2].ToLower().Contains("y")==true)
{
NewDate = DateSplit[0] + "/" + DateSplit[1] + "/" + DateSplit[2];
}
else if (Patternvalue[0].ToLower().Contains("y") == true && Patternvalue[1].ToLower().Contains("m")==true && Patternvalue[2].ToLower().Contains("d")==true)
{
NewDate = DateSplit[2] + "/" + DateSplit[0] + "/" + DateSplit[1];
}
else if (Patternvalue[0].ToLower().Contains("y") == true && Patternvalue[1].ToLower().Contains("d")==true && Patternvalue[2].ToLower().Contains("m")==true)
{
NewDate = DateSplit[2] + "/" + DateSplit[1] + "/" + DateSplit[0];
}
date = DateTime.Parse(NewDate, Thread.CurrentThread.CurrentCulture);
}
catch (Exception ex)
{
}
finally
{
}
return date;
}
對不起,這不工作.DateTimeFormatInfo dtfi = new DateTimeFormatInfo(); dtfi.ShortDatePattern = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern; dtfi.DateSeparator = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DateSeparator; result = Convert.ToDateTime(result,dtfi).ToString(System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern); – Lokesh 2011-04-08 03:57:18
因爲當你設置'dtfi.ShortDatePattern = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;'它會得到你的機器當前的DatePattern,而不是原始日期字符串的模式:) – 2011-04-08 04:02:02
如果你設置區域和語言設置爲英文[英文],短日期格式爲dd/MM/yyyy。 – Lokesh 2011-04-08 04:04:14