我如何获取一个月中的所有工作日和日期 [英] How do i get all the weekdays and dates in a month

查看:174
本文介绍了我如何获取一个月中的所有工作日和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取所选月份的所有工作日以及星期一或星期二之类的日期?这怎么可能?有我不知道的内置功能吗?

I am trying to get all the weekdays of the selected Month along with the day like Monday or Tuesday, etc?? How is it possible? is there an inbuilt function that i do not know?

这在C#2.0中,使用VS 2005在asp.net中

This is in C# 2.0, asp.net using VS 2005

推荐答案

我不保证leap年,夏令时,特殊日子,

I make no guarantees about leap years, daylight savings jumps, special days, Shanghai 1927, etc.

public List<DateTime> getWeekdatesandDates(int Month, int Year)
{
    List<DateTime> weekdays = new List<DateTime>();

    DateTime firstOfMonth = new DateTime(Year, Month, 1);

    DateTime currentDay = firstOfMonth;
    while (firstOfMonth.Month == currentDay.Month)
    {
        DayOfWeek dayOfWeek = currentDay.DayOfWeek;
        if (dayOfWeek != DayOfWeek.Saturday && dayOfWeek != DayOfWeek.Sunday)
            weekdays.Add(currentDay);

        currentDay = currentDay.AddDays(1);
    }

    return weekdays;
}

生成的 DateTime 对象具有 DayOfWeek 属性,您可以检查该属性是否是星期一至星期五.

The resultant DateTime objects have a DayOfWeek property which you can check to see if it's Monday through Friday.

这篇关于我如何获取一个月中的所有工作日和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆