Click here to Skip to main content
14,971,296 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
I have a textbox in which i take date input from user. But I can't save that date to my db as it shows error that invalid date format. How can I change the datetime format of asp.net(C#) to mysql db format which is yyyy-mm-dd?
Posted

Convert.ToDateTime(txtDate.Text).ToString("yyyy-MM-dd")
   
Comments
[no name] 27-Aug-12 8:42am
   
my 5
Member 9722231 7-Jun-13 9:02am
   
my problem solved! Thanks
mute_gemini 27-Feb-14 1:50am
   
my problem solved! Thanks
Member 12391481 14-Mar-16 13:57pm
   
Tks verymuch
The first step shown in m@dhu#s answer is correct: convert the string you get from the aspxpage to a DateTime value. But then, use a parameterized query to get the data into your database! By string concatenation, you would open it for SQL injection attacks.
Do you know how such an attack works? Well, read a thread in the hall of shame: http://www.codeproject.com/Feature/WeirdAndWonderful.aspx?msg=4335687#xx4335687xx[^] (the information on the injection attack comes late - and: they haven't fixed it yet, you can try your skills...)
   
The Solution 01 is correct. But the standard way of doing this is using a "DateTimePicker". You can easily find it under the "Common Controls" in ToolBox. To format the date according to the given format, follow the steps given below:

1.select the "DateTimePicker"
2.press "F4" to see the properties window
3.change the property of "CustomFormat" as yyyy-MM-dd
4.change the property of "Format" as Custom


If you have not defined the relevant database field as a datetime field, don't forget to cast the output of "DateTimePicker" as it demands.
   
You must convert date before inserting.
You can convert it from C#
http://www.dotnetperls.com/datetime-format

OR in sql
http://www.sqlusa.com/bestpractices/datetimeconversion/
   
var userdateformat = DateTime.ParseExact("20120827", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
   
Try this
C#
DateTime dt = DateTime.ParseExact(dateString, "ddMMyyyy", CultureInfo.InvariantCulture);
dt.ToString("yyyyMMdd");
   
Comments
bhagirathimfs 27-Aug-12 9:13am
   
Is it showing error?
try this...
DateTime dtTimeObj = Convert.ToDateTime("9/14/2009 12:00:00 AM");
dtTimeObj.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
   
Try This Friend...
DateTime dtTimeObj = Convert.ToDateTime("9/14/2009 12:00:00 AM");
dtTimeObj.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
   
For SQL Convert(varchar(20), ColumnName, 20) AS ColumnSameName

I use this to import data to a datagridview in c# format the date at query and display the datetime

Then I get that information and export to a CSV - Otherwise the format will show dd/mm/yyyy HH:mm tt

Result:

yyyy-MM-dd HH:mm:ss

2014-11-21 02:03:04
   
Comments
King Fisher 22-Nov-14 1:31am
   
you are trying to solve 4 year old Questions
   
Comments
King Fisher 22-Nov-14 1:31am
   
you are trying to solve 4 year old Questions

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