Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change date format from DD/MM/YYYY or MM/DD/YYYY to YYYY-MM-DD?

Tags:

c#

i need to change the format of my date string using C#

from : "06/16/2010"or "16/06/2010"

to : "2010-06-16"

can you please help me achieve this

thanks

like image 927
bachchan Avatar asked Jun 19 '10 07:06

bachchan


3 Answers

If you already have it as a DateTime, use:

string x = dt.ToString("yyyy-MM-dd");

See the MSDN documentation for more details. You can specify CultureInfo.InvariantCulture to enforce the use of Western digits etc. This is more important if you're using MMM for the month name and similar things, but it wouldn't be a bad idea to make it explicit:

string x = dt.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

If you have a string to start with, you'll need to parse it and then reformat... of course, that means you need to know the format of the original string.

like image 142
Jon Skeet Avatar answered Nov 17 '22 05:11

Jon Skeet


The following will do.

string datestring = DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
like image 26
Thurein Avatar answered Nov 17 '22 05:11

Thurein


String dt = Date.Now.ToString("yyyy-MM-dd");

Now you got this for dt, 2010-09-09

like image 1
user304018 Avatar answered Nov 17 '22 04:11

user304018