.NET Globalization IsWeekend? [duplicate]

2019-06-24 07:11发布

问题:

Possible Duplicate:
Finding weekend days based on culture

Is there any locale info available in .NET to know if a given date falls on a weekday or a weekend?

i would use the function:

public Boolean IsWeekend(DateTime value, CultureInfo cultureInfo)
{
   return (value.DayOfWeek == DayOfWeek.Saturday || value.DayOfWeek == DayOfWeek.Sunday);
}

But it returns invalid results for some cultures.

Has .NET already done the work to compile what days are weekends for all cultures?

回答1:

It is pretty interesting question and the answer seems to be no, there is no such function in .Net. Of course you can use DateTimeFormatInfo's FirstDayOfWeek Property, but it is not good enough.

It will just tell you that there is high chance that preceding day was the weekend day. However, it is not very helpful (what about the other one and...), as there are countries that have just one day weekend. Also, in Muslim countries, they typically have Friday as a weekend day, but... Some of them have also Thursday, some Saturday, and I am sure I heard about country that has Friday and Sunday off. In this case, it would be quite hard to talk about a weekend...

I took a look at CLDR Charts, but I can't find appropriate information. It seems you are on your own.
In theory, you may think of using ICU's Calendar class which has isWeekend() method but importing a dll just to use one class out of it sounds pretty bad.