Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert any date format to yyyy-MM-dd

Tags:

c#

My app parses a string data, extracts the date and identify the format of the date and convert it to yyyy-MM-dd.

The source date could be anything lime dd-mm-yyyy, dd/mm/yyyy, mm-dd-yyyy, mm/dd/yyyy or even yyyy-MM-dd.

Other than attempting different permutations and combinations using switch case, is there any other efficient way to do it?

string sourceDate = "31-08-2012";
String.Format("{0:yyyy-MM-dd}", sourceDate);

The above code simply returns the same sourceDate "31-08-2012".

like image 667
GaneshT Avatar asked Sep 04 '12 05:09

GaneshT


People also ask

How do I change a date format from YYYY to MM DD in Excel?

First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay. It will format the dates you specify.

How do I change the date format from Yyyymmdd to yyyy-mm-dd in SQL?

Convert Char 'yyyymmdd' back to Date data types in SQL Server. Now, convert the Character format 'yyyymmdd' to a Date and DateTime data type using CAST and CONVERT. --A. Cast and Convert datatype DATE: SELECT [CharDate], CAST([CharDate] AS DATE) as 'Date-CAST', CONVERT(DATE,[CharDate]) as 'Date-CONVERT' FROM [dbo].


1 Answers

string DateString = "11/12/2009";
IFormatProvider culture = new CultureInfo("en-US", true); 
DateTime dateVal = DateTime.ParseExact(DateString, "yyyy-MM-dd", culture);

These Links might also Help you

DateTime.ToString() Patterns

String Format for DateTime [C#]

like image 159
Krishna Thota Avatar answered Oct 17 '22 19:10

Krishna Thota