Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL DateTime
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 11-Oct-10 2:32am
sazib373
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Convert.ToDateTime(txtDate.Text).ToString("yyyy-MM-dd")
  Permalink  
Comments
Member 9722231 at 7-Jun-13 9:02am
   
my problem solved! Thanks
mute_gemini at 27-Feb-14 1:50am
   
my problem solved! Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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...)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

Try This Friend...
DateTime dtTimeObj = Convert.ToDateTime("9/14/2009 12:00:00 AM");
dtTimeObj.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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/
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

var userdateformat = DateTime.ParseExact("20120827", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Try this
DateTime dt = DateTime.ParseExact(dateString, "ddMMyyyy", CultureInfo.InvariantCulture);
dt.ToString("yyyyMMdd");
  Permalink  
Comments
bhagirathimfs at 27-Aug-12 9:13am
   
Is it showing error?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

try this...
DateTime dtTimeObj = Convert.ToDateTime("9/14/2009 12:00:00 AM");
dtTimeObj.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 10

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
  Permalink  
Comments
King Fisher at 3 days ago
   
you are trying to solve 4 year old Questions
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 11

  Permalink  
Comments
King Fisher at 3 days ago
   
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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 295
1 OriginalGriff 274
2 Shweta N Mishra 216
3 PIEBALDconsult 184
4 CPallini 155
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 21 Nov 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100