在MM / DD / YYYY格式转换的日期格式 [英] Converting any date format in MM/DD/YYYY format

查看:241
本文介绍了在MM / DD / YYYY格式转换的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同的日期格式的困难。我在使用 asp.net C#创建了一个Web应用程序。我已经使用 JQuery的日历控件,显示日期沿与时间。

我再解析用这个日期 DateTime.Parse()。如果服务器被设置与印度的日期格式,那么应用程序生成转换错误 字符串未被识别为有效的DateTime。

有没有一种方法,以任何日期格式转换为 MM / DD / YYYY 格式随着时间的?
我知道我可以使用 DateTime.ParseExact()但同样会坚持用特定的格式。

任何帮助是极大AP preciated。

感谢分享您的智慧。


解决方案

  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(),出DT);

I am having difficulty with different date formats. I am having a web application created using asp.net c#. I have used JQuery Calendar control which shows date along-with time.

I am then parsing this date using DateTime.Parse(). If the server is set with indian date format then the application generates conversion error "String was not recognized as a valid DateTime."

Is there a way to convert any date format into MM/DD/YYYY format along with time? I know i can use DateTime.ParseExact() but again it will stuck with a particular format.

Any help is greatly appreciated.

Thanks for sharing you wisdom.

解决方案

Convert.ToDateTime(DateTime.Now, CultureInfo.GetCultureInfo("en-US")).ToString("MM/dd/yyyy hh:MM:ss");

You can replace en-US by hi-IN for Hindi Date or as per you like. please follow link. for Culture Info.

http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.80).aspx

If you are unsure of Culture then use below line.

Convert.ToDateTime(DateTime.Now, CultureInfo.CurrentCulture).ToString("MM/dd/yyyy hh:MM:ss");

reference here http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture.aspx

Other Method you can use is.

DateTime dt;
DateTime.TryParse(DateTime.Now.ToString(), out dt);

这篇关于在MM / DD / YYYY格式转换的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆