How to convert a Datetime string to a current culture datetime string
I have string say "12/1/2011" in English US culture my current machine culture is English Uk which is "dd/mm/yyyy" format. How to convert the 12/1/2011 to 1/12/2011. I have tried the below format.
System.DateTime.Parse(result,System.Threading.Thread.CurrentThread.CurrentCulture)
.ToString(System.Threading.Thread.CurrentT开发者_运维技巧hread.CurrentCulture.DateTimeFormat.ShortDatePattern)
but i could not able to see any output.
-Lokesh.
DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat;
DateTimeFormatInfo ukDtfi = new CultureInfo("en-GB", false).DateTimeFormat;
string result = Convert.ToDateTime("12/01/2011", usDtfi).ToString(ukDtfi.ShortDatePattern);
This will do the trick ^^
This works for me:
string myTime = DateTime.Parse("12/1/2011")
.ToString(CultureInfo.GetCultureInfo("en-GB").DateTimeFormat.ShortDatePattern);
var culture = new CultureInfo( "en-GB" );
var dateValue = new DateTime( 2011, 12, 1 );
var result = dateValue.ToString( "d", culture ) );
DateTime dateValue;
CultureInfo culture = CultureInfo.CurrentCulture;
DateTimeStyles styles = DateTimeStyles.None;
DateTime.TryParse(datetimestring,culture, styles, out dateValue);
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;
}
This works for me,
DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat;
DateTimeFormatInfo ukDtfi = new CultureInfo("en-GB", false).DateTimeFormat;
string result = Convert.ToDateTime("26/09/2015",ukDtfi).ToString(usDtfi.ShortDatePattern);
精彩评论