Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.today() runs differently in administrator

Tags:

c#

datetime

I live in Canada, the DateTime.Today format is recorded as dd/mm/yyyy. It is strange when I run the application as administrator, it records the DateTime.Today format as mm/dd/yyyy.

Is there anyway I can do a check? Or it is the computer datetime setting?

like image 488
Chelvan Mei Avatar asked Jan 28 '23 19:01

Chelvan Mei


1 Answers

A DateTime object isn't internally represented in days and months, so it doesn't actually have any native format per se. (There also isn't a DateTime.Today() method, but rather a property DateTime.Today)

A DateTime (which DateTime.Today returns an instance of) does have a ToString() method which is declared as:

public override string ToString()
{
  return DateTimeFormat.Format(this, (string) null, DateTimeFormatInfo.CurrentInfo);
}

That CurrentInfo is going to be different for different users on the machine. Some operations will implicitly call ToString() (eg Console.WriteLine)

The good news is that there are overloads for ToString(), so you can do something like

Console.Write(DateTime.Today.ToString("dd/MM/yyyy"));

(Or any IFormatProvider)

like image 79
Adam G Avatar answered Feb 02 '23 08:02

Adam G