用javascript取得某一年的第一个星期一的日期

在c#中写法如下:

             DateTime day = DateTime.Parse(year.ToString() + "-1-1");
                while (day.DayOfWeek != DayOfWeek.Monday)
                {
                    day = day.AddDays(1);
                }
                DateTime firstDate = Convert.ToDateTime(day.ToString("yyyy/MM/dd"));

求在javascript中写法?


慕慕森
浏览 490回答 2
2回答

MMTTMM

function&nbsp;get(&nbsp;year&nbsp;){ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;d&nbsp;=&nbsp;new&nbsp;Date(); &nbsp;&nbsp;&nbsp;&nbsp;d.setFullYear(&nbsp;year&nbsp;&nbsp;,&nbsp;0&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;for(&nbsp;var&nbsp;i&nbsp;=&nbsp;1&nbsp;;&nbsp;i&nbsp;<=&nbsp;31&nbsp;;&nbsp;i++&nbsp;){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d.setDate(&nbsp;i&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;d.getDay()&nbsp;==&nbsp;1&nbsp;)&nbsp;break; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;d.setFullYear(&nbsp;year&nbsp;,&nbsp;0&nbsp;,&nbsp;i&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;alert(&nbsp;d.toLocaleDateString()&nbsp;&nbsp;+&nbsp;"&nbsp;Rain-man&nbsp;:"&nbsp;+&nbsp;year&nbsp;+&nbsp;'-'&nbsp;+&nbsp;'1-'&nbsp;+&nbsp;i&nbsp;); } get(&nbsp;2009&nbsp;);//调用函数,参数是2009年返回结果:2009年1月5日 Rain-man :2009-1-5

达令说

function get(year) {&nbsp;&nbsp;&nbsp; var d = new Date(year, 1, 1);&nbsp;&nbsp;&nbsp; var day = d.getDay(); //获取1月1号是星期几&nbsp;&nbsp;&nbsp; d.setDate((8 - day) % 7 + 1);&nbsp;&nbsp;&nbsp; return d;}
打开App,查看更多内容
随时随地看视频慕课网APP