Finding weekend days based on culture

后端 未结 5 717
礼貌的吻别
礼貌的吻别 2020-12-19 02:34

Is there a way to find the days that constitute a weekend or workweek based on different cultures using the .NET framework? For example, some Muslim countries have a workwee

5条回答
  •  时光取名叫无心
    2020-12-19 03:25

    The below code will work till the time last 2 days are considered as weekends in cultures.

    :)

    /// 
    /// Returns true if the specified date is weekend in given culture
    /// is in. 
    /// 
    public static bool IsItWeekend(DateTime currentDay, CultureInfo cultureInfo)
    {
        bool isItWeekend = false;
    
        DayOfWeek firstDay = cultureInfo.DateTimeFormat.FirstDayOfWeek;
    
        DayOfWeek currentDayInProvidedDatetime = currentDay.DayOfWeek;
    
        DayOfWeek lastDayOfWeek = firstDay + 4;
    
        if (currentDayInProvidedDatetime == lastDayOfWeek + 1 || currentDayInProvidedDatetime == lastDayOfWeek + 2)
            isItWeekend = true;
    
        return isItWeekend;         
    }
    

    Amit Tonk

提交回复
热议问题