Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
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.
Posted

C#
// 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");
 
Share this answer
 
This should help.[^]

Hope it is useful.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900