Convert DateTime in C# to yyyy-MM-dd format and Store it to MySql DateTime Field

C#MysqlDateDatetime

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

We can use the below its very simple.

Date.ToString("yyyy-MM-dd");

Solution 3 - C#

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.

Solution 4 - C#

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"));

Solution 5 - C#

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.

Solution 6 - C#

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

SomeDate.Value.GetDateTimeFormats()[5]

GetDateTimeFormats

Solution 7 - C#

Try this!

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahul GokaniView Question on Stackoverflow
Solution 1 - C#AlexView Answer on Stackoverflow
Solution 2 - C#sanjay.arora29View Answer on Stackoverflow
Solution 3 - C#DrewbieDooView Answer on Stackoverflow
Solution 4 - C#Asanka MadushanView Answer on Stackoverflow
Solution 5 - C#KifoPLView Answer on Stackoverflow
Solution 6 - C#YKCView Answer on Stackoverflow
Solution 7 - C#PrasannaView Answer on Stackoverflow