Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert system date to M/d/yyyy format irrespective of system format using C#

Tags:

date

c#

datetime

How can I get today's date in M/d/yyyy format irrespective of system format of date using C#?

DateTime.Now.Tostring('M/d/yyyy')

is working only if the system date is in format dd/MM/yyyy or M/dd/yyyy but is not working in case yyyy-MM-dd format.

Eg:

if system date is 2013-06-26 then DateTime.Now.Tostring('M/d/yyyy') is converting the date into 06-26-2013 but not in 06/26/2013

like image 380
r.bhardwaj Avatar asked Nov 30 '22 12:11

r.bhardwaj


1 Answers

Use CultureInfo.InvariantCulture to enforce / as date separator:

DateTime.Now.ToString("M/d/yyyy", CultureInfo.InvariantCulture) 

Ideone

like image 155
Tim Schmelter Avatar answered May 17 '23 13:05

Tim Schmelter