简体   繁体   中英

how to get DateTime format to provide Date Month and Year based on Culture of the website?

I am trying to fetch the date based on content Culture of the website in a specific format.

I want to have the Date in the format Date Month Year

de-DE Culture        1. Oktober 2008
en-US Culture        October 01, 2008
es-ES Culture        01 de octubre de 2008
fr-FR Culture        1 octobre 2008

I have tried datetimeObject.ToString("m", contentCultureObject) which results in

de-DE Culture                               01 Oktober
en-US Culture                               October 01
es-ES Culture                               01 octubre
fr-FR Culture                                1 October

I have also tried datetimeObject.ToString("Y", contentCultureObject) which results in

de-DE Culture                             Oktober 2008
en-US Culture                            October, 2008
es-ES Culture                          octubre de 2008
fr-FR Culture                             octobre 2008

Basically I want to Combine both "m" and "Y" formats to achieve the desired output. Please help.

//you can write your desired Culture 

CultureInfo ci = CultureInfo.CreateSpecificCulture("es-ES");
    ci.DateTimeFormat.ShortDatePattern = "d MMMM yyyy";
    ci.DateTimeFormat.FullDateTimePattern = "d MMMM yyyy h:m:ss";
    Thread.CurrentThread.CurrentCulture = ci;

//then call
datetimeObject.ToString();

I was able to do it by replacing the FullDateTimePattern string.

Here's what I did:

string currentCulturePattern = contentCultureObject.DateTimeFormat.LongDatePattern;
string requiredPattern = currentCulturePattern.Replace("dddd,", String.Empty).Replace("dddd", String.Empty).Trim();
dateTimeObject.ToString(requiredPattern, contentCultureObject);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM