如何将日期格式从DD/MM/YYYY更改为YYYY/MM/DD [英] How to Change Dateformat from DD/MM/YYYY to YYYY/MM/DD
问题描述
我从DD/MM/YYYY格式的Ajax日历控件中获取DateOfBirth.但是我需要以YYYY/MM/DD的格式将DOb插入到MySql Server数据库中. DOB已分配到文本框.
如何将日期格式更改为YYYY/MM/DD.
DateTime DOB =新的DateTime();
DOB = Convert.ToDateTime(Txt_dob.Text);
字符串dob = Convert.ToString(DOB); ;
DateTime dobdt =新的DateTime();
dobdt = DateTime.ParseExact(dob,"yyyy-MM-dd HH:mm tt",null);
在以下面的方式进行转换时会出现如下错误:
无法将字符串识别为有效的DateTime.
帮帮我..如果还有其他方法...
谢谢.
Hi,
I am getting DateOfBirth from Ajax calendar control in the form of DD/MM/YYYY format.But i need to insert the DOb in to MYSql Server database in the format of YYYY/MM/DD. DOB is assigned into Textbox.
How i can change the date format into YYYY/MM/DD.
DateTime DOB = new DateTime();
DOB = Convert.ToDateTime(Txt_dob.Text);
string dob = Convert.ToString(DOB); ;
DateTime dobdt = new DateTime();
dobdt = DateTime.ParseExact(dob, "yyyy-MM-dd HH:mm tt",null);
while converting like below manner am getting error like this:
String was not recognized as a valid DateTime.
Help me..IF any another methods also appriciated...
Thanks.
推荐答案
// dateOfBirth will simulate your text from Txt_dob.Text
string dateOfBirth = DateTime.Now.ToString("dd/MM/yyyy");
DateTime dateTimeInstance = DateTime.ParseExact(
dateOfBirth,
"dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
// You now have a DateTime instance.
// If you need this as a string in MySQL format
string mySqlString = dateTimeInstance.ToString("yyyy/MM/dd");
此 [ ^ ]
希望它有用.
This should help.[^]
Hope it is useful.
这篇关于如何将日期格式从DD/MM/YYYY更改为YYYY/MM/DD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!