如何处理日期时间格式 [英] How to deal with datetime format

查看:74
本文介绍了如何处理日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我在用户界面上使用datetime格式为"dd/MM/yyyy".
当区域设置"中的日期格式设置为dd/MM/yyyy时,此方法可以正常工作,但如果我将日期格式设置为MM/dd/yyyy或其他任何格式,则会引发类似无效的日期时间格式"的错误.

我的机器日期格式为MM dd yyyy,我的代码如下:

In my project I have used datetime format as ''dd/MM/yyyy'' on user interface.
This will works ok when date format in Regional settings is set to dd/MM/yyyy, but if I set date format to MM/dd/yyyy, or any other, it will raise an error like ''invalid Datetime format''.

My machines date format is MM dd yyyy, and my code is as below:

string dispDate = nodeList[0].ChildNodes[i].InnerText;
//this line assigns date '22/12/2010 6:29:57 PM' to dispDate which is in 'dd MM yyyy' format
dispDate=string.Format("{0:MM dd yyyy hh:mm:ss tt}",dispDate);
//Here i m converting it to 'MM dd yyyy' format
dtpBillDate.Value  = Convert.ToDateTime(dispDate);
//Here i get the problem even if i convert the value to 'MM dd yyyy' format(machine format) 



如何解决此问题?



How to solve this problem?

推荐答案

请参阅此处:
See here: Formatting a DateTime for display - format string description[^]
You can use the same formats for DateTime.ParseExact:
DateTime dt = DateTime.ParseExact(myInputString, "MM/dd/yyyy", CultureInfo.InvariantCulture);


在此链接中查看答案:

http://www.codeproject.com/Answers/124702/DateTime-Array-SOLVED. aspx#answer3 [ ^ ]
Check out the answers in this link:

http://www.codeproject.com/Answers/124702/DateTime-Array-SOLVED.aspx#answer3[^]


这篇关于如何处理日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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