在一年的第一周计算星期一的最简单方法是什么 [英] whats the simplest way to calculate the Monday in the first week of the year

查看:46
本文介绍了在一年的第一周计算星期一的最简单方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递一年并返回代表第一周的第一个星期一的日期

i want to pass in a year and get a date back that represents the first monday of the first week

所以:


  • 如果在 2011 中通过,我会回来 2011年1月3日

  • 如果在 2010 中通过,我会回来 2010年1月4日

  • If a passed in 2011, i would get back Jan 3, 2011
  • If a passed in 2010, i would get back Jan 4, 2010

推荐答案

private DateTime GetFirstMondayOfYear(int year)
{
    DateTime dt = new DateTime(year, 1, 1);

    while (dt.DayOfWeek != DayOfWeek.Monday)
    {
        dt = dt.AddDays(1);
    }

    return dt;
}

这篇关于在一年的第一周计算星期一的最简单方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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