如何将字符串转换为.net中的datetime [英] how to convert string to datetime in .net
问题描述
嘿,嗯,
i有一个日期时间字符串格式,现在我想转换相同的日期时间格式,在字符串我有以下字符串....(12.01.2011)dd.mm.yyyy格式,现在我希望以datetime格式转换此格式,因为我想将其存储在数据库中,其数据类型为datetime ....
hey guys, i have a datetime in string format and now i want to convert the same in datetime format, in the string i have following string.... (12.01.2011) dd.mm.yyyy format, now i want this format to be converted in datetime format because i want to store this in database which has a field whose datatype is datetime....
请回复为尽快
感谢并要求
阿巴斯电子瓦拉。
Please reply as soon as possible. Thanks and regards Abbas electricwala.
推荐答案
使用DateTime.Parse并注意区域设置。您可以通过提供您自己的CultureInfo来绕过区域设置。我不知道你使用哪种语言,但我的语言(丹麦语)支持你的日期格式(dd.mm.yyyy)。因此,我使用以下语法:
Use DateTime.Parse and be aware of the regional settings. You can bypass regional settings by providing your own CultureInfo. I don't know which language you use, but my language (danish) support your date format (dd.mm.yyyy). Thus, I use the following syntax:
string inputDate = "31.12.2001";
CultureInfo cultureInfo = new CultureInfo("da-DK");
DateTime parsedDate = DateTime.Parse(inputDate, cultureInfo);
或者,您可以拆分输入字符串,并构造一个新的Date。
Alternatively, you can split the input string, and construct a new Date.
问候,
Morten
Regards, Morten
这篇关于如何将字符串转换为.net中的datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!