本文介绍了一个月的最后一个星期一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


请问如何获取每月的最后一个星期一?
感谢

Hi,
How do I get the last monday of the month please?
Thanks

推荐答案


public static DateTime LastMonday(int year, int month)
{
    DateTime dt;
    if (month < 12)
        dt = new DateTime(year, month + 1, 1);
    else
        dt = new DateTime(year + 1, 1, 1);
    dt = dt.AddDays(-1);
    while (dt.DayOfWeek != DayOfWeek.Monday) dt = dt.AddDays(-1);
    return dt;
}


void Main()
{
    DateTime date = LastDayOfMonth(2012, 6, DayOfWeek.Monday);
    
    //date = 6/25/2012
}
    
public DateTime LastDayOfMonth(int year, int month, DayOfWeek dayOfWeek){
    DateTime lastDateOfMonth = new DateTime(year, month,
                                 DateTime.DaysInMonth(year,month));
    return lastDateOfMonth.AddDays(lastDateOfMonth.DayOfWeek < dayOfWeek ? 
    			dayOfWeek - lastDateOfMonth.DayOfWeek -  7 : 
    			dayOfWeek - lastDateOfMonth.DayOfWeek);
}


这篇关于一个月的最后一个星期一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 02:42