简体   繁体   中英

Find date (all languages)

I hit the issue when I need to find first occurrence of date in a html source. This can be "January 24, 2000" as well as 24 Januar 2000 (slovak language) or any other language date format.

you may know a library that would be able to do this. I don't, google didn't help :(

I can't imagine how regex can do this as I would need to do a rule for all languages manually

The CultureInfo class is what you're looking for. Use dt.ToString("D", c) to pass the cultureinfo and get your format(btw, same as dt.ToLongDateString ).

Have a look at the standard date and time format strings .

You can use CultureInfo.GetCultures to get all supported cultures.

DateTime dt = new DateTime(2000, 1, 24);
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
string allTranslatedJanuaries = 
    string.Join(Environment.NewLine, cultures.Select(c =>
        String.Format("{0}: {1}", c.EnglishName, dt.ToString("D", c))));

Here's a demo: http://ideone.com/6ypqJE

On my server 352 cultures are installed, upon ideone only 112.

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