Sylla
Sylla

Reputation: 77

How can I get datetime today date from C# with format 'dd-MMM-yy'

I want to get today date from C# and I am using this line of code:

 string result = DateTime.Today.ToString("dd-MMM-yy");

The code is working fine on my localhost, but when I publish it to a Windows Server, it always returns this format of date:

28-juil.-22

But it should be like this 28-jul-22

Can someone please help me?

Upvotes: 0

Views: 495

Answers (1)

Konrad Kacperczyk
Konrad Kacperczyk

Reputation: 246

You should use Invariant culture when formatting DateTime string:

string result = DateTime.Today.ToString("dd-MMM-yy", CultureInfo.InvariantCulture);

For further and more detailed information about what is and how Invariant culture works please refer to documentation https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.invariantculture?view=net-6.0

Upvotes: 1

Related Questions