如何获得第一天和最后一天的周数和年?

时间:2022-10-21 10:14:47

In java-script i want to get first week of the date and last week of the date by week number and year ....

在java script我想第一个星期的日期和上周周号码和日期的年....

For example if i give the input like

例如,如果我输入

2(week),2012

2(一周),2012

it should return the values

它应该返回值

2012-01-08 and 2012-01-14

2012-01-08和2012-01-14

4 个解决方案

#1


7  

Try this:

试试这个:

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)

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

n1包含了一周的第一天n2包含了一周的最后一天。

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

至于常数:604800000是1周,毫秒518400000是6天

#2


3  

A little change to @bardiir 's answer, if the first day of the year is not Sunday(or Monday) that result is not correct. You should minus the number of the first day.

@bardiir的回答稍微改变一下,如果一年的第一天不是星期天(或者星期一),那么结果就不正确。你应该减去第一天的天数。

Changed code

改变了代码

firstDay = new Date(2015, 0, 1).getDay();
console.log(firstDay);
var year = 2015;
var week = 67;
var d = new Date("Jan 01, "+year+" 01:00:00");
var w = d.getTime() -(3600000*24*(firstDay-1))+ 604800000 * (week-1)
var n1 = new Date(w);
var n2 = new Date(w + 518400000)

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

if you wish the first is Sunday, change the (firstDay-1) to

如果你希望第一天是星期天,把第一天改成星期天

#3


1  

you can check and try with below link.

你可以检查和尝试下面的链接。

How to get first and last day of the week in JavaScript

如何在JavaScript中获得每周的第一天和最后一天

It will may helpful to you.

这可能对你有帮助。

Thanks.

谢谢。

#4


-1  

   dt = new Date();
   var firstDateOfWeek=(dt.setDate(dt.getDate()-dt.getDay()));
   var lastDateOfWeek=(dt.setDate(dt.getDate()+6-dt.getDay()));

#1


7  

Try this:

试试这个:

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)

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

n1包含了一周的第一天n2包含了一周的最后一天。

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

至于常数:604800000是1周,毫秒518400000是6天

#2


3  

A little change to @bardiir 's answer, if the first day of the year is not Sunday(or Monday) that result is not correct. You should minus the number of the first day.

@bardiir的回答稍微改变一下,如果一年的第一天不是星期天(或者星期一),那么结果就不正确。你应该减去第一天的天数。

Changed code

改变了代码

firstDay = new Date(2015, 0, 1).getDay();
console.log(firstDay);
var year = 2015;
var week = 67;
var d = new Date("Jan 01, "+year+" 01:00:00");
var w = d.getTime() -(3600000*24*(firstDay-1))+ 604800000 * (week-1)
var n1 = new Date(w);
var n2 = new Date(w + 518400000)

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

if you wish the first is Sunday, change the (firstDay-1) to

如果你希望第一天是星期天,把第一天改成星期天

#3


1  

you can check and try with below link.

你可以检查和尝试下面的链接。

How to get first and last day of the week in JavaScript

如何在JavaScript中获得每周的第一天和最后一天

It will may helpful to you.

这可能对你有帮助。

Thanks.

谢谢。

#4


-1  

   dt = new Date();
   var firstDateOfWeek=(dt.setDate(dt.getDate()-dt.getDay()));
   var lastDateOfWeek=(dt.setDate(dt.getDate()+6-dt.getDay()));