如何从周数和年份获得一周的第一个日期和最后一个日期? [英] How to get first date and last date of the week from week number and year?

查看:75
本文介绍了如何从周数和年份获得一周的第一个日期和最后一个日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,我想获得本周的第一个日期和星期的最后一个日期,仅按周数和年份。

In JavaScript I want to get first date of the week and last date of the week by week number and year only.

例如,如果我输入的是:

For example if I my input is:

2(星期),2012

然后我的输出应该是:

2012-01-08和2012-01-14

推荐答案

试试这个:

var year = 2012;
var week = 2;
var d = new Date("Jan 01, " + year + " 01:00:00");
var w = d.getTime() + 604800000 * (week - 1);
var n1 = new Date(w);
var n2 = new Date(w + 518400000)

console.log(n1);
console.log(n2);

n1包含一周的第一天

n2包含一周的最后一天

n1 contains the first day of the week
n2 contains the last day of the week

至于常数:

604800000是一周(毫秒)

518400000是六天

As for the constants:
604800000 is one week in milliseconds
518400000 is six days

这篇关于如何从周数和年份获得一周的第一个日期和最后一个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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