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

Time:07-29

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?

CodePudding user response:

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://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.invariantculture?view=net-6.0

  • Related