Home > Software engineering >  DateTime.ToLongDateString with different culture
DateTime.ToLongDateString with different culture

Time:10-28

How can I convert a date(DateTime) to a string in a long format with the culture I need?

But the method(DateTime.ToLongDateString()) of converting date to long format string does not accept culture.

Long date string: "Wednesday, May 16, 2001"

In short, you can do this:

DateTime date = DateTime.Now;
date.ToString(CultureInfo.CreateSpecificCulture("en-US"));

CodePudding user response:

ToLongDateString is using the format info's LongDatePattern, which is "dddd, MMMM d, yyyy".

So you can use:

date.ToString("dddd, MMMM d, yyyy", CultureInfo.CreateSpecificCulture("en-US"));

Or simpler, use the long date ("D") format specifier "D":

date.ToString("D", CultureInfo.CreateSpecificCulture("en-US"));

Result: Thursday, October 27, 2022

  • Related