61

I am trying to convert DateTime format to yyyy-MM-dd format and store it to DateTime object. But it gives me the System DateTime format that is MM/dd/yyyy.
I am using following code to convert.

string dateTime = DateTime.Now.ToString();
string createddate = Convert.ToDateTime(dateTime).ToString("yyyy-MM-dd h:mm tt");         
DateTime dt = DateTime.ParseExact(createddate, "yyyy-MM-dd h:mm tt",CultureInfo.InvariantCulture);

but non of the above line converts into the specified format.
Can any one help to solve this.

I am getting the DateTime from one application and passing this object to other application and That application is storing that date into MySql's DateTime field which is in the format "yyyy-MM-dd".
This is why I have posted this question.

Project 1 has class from that I am getting the date. and the processor class which is the middle ware of the application it processes the DateTime format to convert in specific format. And passes to the Other project which consumes the DateTime and stores that in the MySql field.

Rahul Gokani
  • 1,688
  • 5
  • 25
  • 43
  • 3
    Why? How it's stored in the object should make any difference in your code. Just use the proper culture when doing any comparisons. – Smeegs Oct 10 '13 at 13:03
  • 6
    `Datatime` dosen't have a format, the format is applied to how you want to display it (`ToString()`) – Bolu Oct 10 '13 at 13:05
  • I am getting the date from other application and pass that date to the other page and from there I am trying to store that date in MySql dateTime field. – Rahul Gokani Oct 10 '13 at 13:05
  • @RahulGokani then you should have specified that in your question. I suggest you update it to reflect this, as well as showing what code you have tried *to assign it* where you *want* to use it. – crashmstr Oct 10 '13 at 13:10
  • 4
    You should really delete this question and ask "How do I properly store a datetime object in a mysql date column". You're focusing on what you perceive as the solution, but you're really just looking at a symptom, not the problem. – Smeegs Oct 10 '13 at 13:14
  • Kinda. All your code is still focused on changing the way the object stores the data (which isn't going to happen). You should be showing the code where the object tries to update the table. – Smeegs Oct 10 '13 at 13:22
  • @Smeegs he is trying to get C# DateTime field in his required format. Which is not straight forward thing. If he has to worry on "How do I properly store a datetime object in a mysql date column" he must get the DateTime object right in his required format in the first place, Which is what the OP is looking for. – Rajshekar Reddy Oct 17 '15 at 07:48

8 Answers8

121

Use DateTime.Now.ToString("yyyy-MM-dd h:mm tt");. See this.

Alex
  • 1,657
  • 1
  • 12
  • 6
  • 3
    I want to store converted date into the DateTime format I am able to get that string value as specified format. – Rahul Gokani Oct 10 '13 at 13:01
  • 1
    You can't change how DateTime will store your date. But when you need to output this date, or to save in database in specific format, use ToString to create needed format – Alex Oct 10 '13 at 13:03
  • @RahulGokani DateTime doesn't store the data in any format like that. Data will be stored in separate integer properties (Month, Year, Date, Hour etc), format is just the string representation. – Fedor Hajdu Oct 10 '13 at 13:04
  • The internal format of the object is not something you can set. You can format it as you want when you convert it to a string as described above. Maybe you could explain what you are trying to accomplish? – automatic Oct 10 '13 at 13:04
  • I don't get why the storage format even matters to you. If you want to print out the Date you can just format it with Alex's suggested way. If you want to work with the Object itself it surely doesn't matter at all. You got all the Hour/Minute/etc. properties for that. – SKull Oct 10 '13 at 13:05
  • @Mathew Yes you are right but I want to map that object with the MySql field which is in the "yyyy-MM-dd" format. and it is not mapping because of the format. of the DateTime – Rahul Gokani Oct 10 '13 at 13:11
29

We can use the below its very simple.

Date.ToString("yyyy-MM-dd");
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
sanjay.arora29
  • 299
  • 1
  • 4
  • 4
  • 1
    He wants to save it to a DateTime object, The above code gives you string, Which you need to convert back to DateTime and that doesn't work straight forward. – Rajshekar Reddy Oct 17 '15 at 07:50
  • 6
    if somebody here gets the following error: "No overload for method ToString takes 1 arguments" then try this: `((DateTime)Date).ToString("yyyy-MM-dd");` – Djeroen Feb 03 '16 at 16:01
6

Have you tried?

var isoDateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;

// "2013-10-10T22:10:00"
 dateValue.ToString(isoDateTimeFormat.SortableDateTimePattern); 

// "2013-10-10 22:10:00Z"    
dateValue.ToString(isoDateTimeFormat.UniversalSortableDateTimePattern)

Also try using parameters when you store the c# datetime value in the mySql database, this might help.

DrewbieDoo
  • 69
  • 1
  • 1
6

Try setting a custom CultureInfo for CurrentCulture and CurrentUICulture.

Globalization.CultureInfo customCulture = new Globalization.CultureInfo("en-US", true);

customCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd h:mm tt";

System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture;

DateTime newDate = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd h:mm tt"));
Asanka Madushan
  • 463
  • 8
  • 17
6

I know this is an old thread, but to all newcomers, there's a new simplified syntax (Intellisense highlighted it for me, not sure how new this feature is, but my guess is .NET 5.0)

DateTime date = DateTime.Now;
string createdDate = $"{date:yyyy-MM-dd}";

Maybe doesn't look simplified in this example, but when concatenating a long message, it's really convenient.

KifoPL
  • 981
  • 7
  • 18
2

GetDateTimeFormats can parse DateTime to different formats. Example to "yyyy-MM-dd" format.

SomeDate.Value.GetDateTimeFormats()[5]

GetDateTimeFormats

Adam V
  • 6,256
  • 3
  • 40
  • 52
YKC
  • 81
  • 1
  • 2
0

Try this!

DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Ticks)
-1

The culture invariant way, best practice:

DateTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64