我对不同的日期格式有困难。我正在使用asp.net c#创建一个web应用程序。我使用了JQuery日历控件,它显示日期和时间。
然后,我将使用DateTime.Parse()解析这个日期。如果服务器设置为印度日期格式,则应用程序生成转换错误"String不被识别为有效的DateTime“。
是否有方法将任何日期格式随时间转换为MM/DD/YYYY格式?我知道我可以使用DateTime.ParseExact(),但是它还是会使用特定的格式。
任何帮助都是非常感谢的。
谢谢你分享智慧。
发布于 2012-04-30 11:06:48
Convert.ToDateTime(DateTime.Now, CultureInfo.GetCultureInfo("en-US")).ToString("MM/dd/yyyy hh:MM:ss");您可以在印地语日期或按您喜欢的方式将en-US替换为hi-IN。请按照链接。关于文化信息。
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.80).aspx
如果您不确定文化,那么使用下面一行。
Convert.ToDateTime(DateTime.Now, CultureInfo.CurrentCulture).ToString("MM/dd/yyyy hh:MM:ss");参考这里http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture.aspx
您可以使用的其他方法是。
DateTime dt;
DateTime.TryParse(DateTime.Now.ToString(), out dt);发布于 2012-04-30 11:11:39
您可以尝试DateTime.Parse或DateTime.TryParse方法。
DateTime.Parse(DateTime.Now).ToString("MM/dd/yyyy hh:mm tt");也请参阅
http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
谢谢
迪普
发布于 2012-04-30 12:01:29
你可以用这段代码。
public static CultureInfo CreateCulture(string shortDateFormat)
{
CultureInfo newCulture = CultureInfo.CreateSpecificCulture("en-US");
newCulture.DateTimeFormat.ShortDatePattern = shortDateFormat;
newCulture.DateTimeFormat.LongDatePattern = shortDateFormat;
return newCulture;
}https://stackoverflow.com/questions/10382632
复制相似问题