使用 JavaScript 的两个日期之间的周数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22859704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:54:23  来源:igfitidea点击:

Number of weeks between two dates using JavaScript

javascript

提问by Cameron

I'm trying to return the number of weeks between two dates using JavaScript.

我正在尝试使用 JavaScript 返回两个日期之间的周数。

So I have the following variables:

所以我有以下变量:

var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();

if(day < 10) { day= '0' + day; }
if(month < 10) { month = '0' + month; }

var dateToday = day + '/' + month + '/' + year;

var dateEndPlacement = '22/06/2014';

I've also prefixed the days and months with 0 if they are less than 10. Not sure if this is the correct way to do this... so alternative ideas would be welcomed.

如果天数和月数小于 10,我还为天数和月份加上前缀 0。不确定这是否是正确的方法......所以其他想法会受到欢迎。

And then I pass these two dates to the following function:

然后我将这两个日期传递给以下函数:

function calculateWeeksBetween(date1, date2) {
    // The number of milliseconds in one week
    var ONE_WEEK = 1000 * 60 * 60 * 24 * 7;
    // Convert both dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();
    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms);
    // Convert back to weeks and return hole weeks
    return Math.floor(difference_ms / ONE_WEEK);
}

However I get the error:

但是我收到错误:

Uncaught TypeError: Object 04/04/2014 has no method 'getTime'

Uncaught TypeError: Object 04/04/2014 has no method 'getTime'

Any ideas what I am doing wrong?

任何想法我做错了什么?

For those that are asking/gonna ask, I'm calling the function like this:

对于那些问/要问的人,我正在调用这样的函数:

 calculateWeeksBetween(dateToday, dateEndPlacement);

回答by Hyman Allan

I would recommend using moment.jsfor this kind of thing.

我建议使用moment.js来处理这种事情。

But if you want to do it in pure javascript here is how I would do it:

但是如果你想用纯 javascript 来做,我会这样做:

function weeksBetween(d1, d2) {
    return Math.round((d2 - d1) / (7 * 24 * 60 * 60 * 1000));
}

Then call with

然后用

weeksBetween(new Date(), new Date(2014, 6, 22));

回答by Kara Brightwell

You are storing your dates as strings ('22/06/2014'). getTimeis a method of Date. You need to create Dateobjects for the dates, and pass those into the function.

您将日期存储为字符串 ( '22/06/2014')。getTime是一种方法Date。您需要Date为日期创建对象,并将它们传递给函数。

var dateToday = new Date(year, month - 1, day);
var dateEndPlacement = new Date(2014, 5, 22);

calculateWeeksBetween(dateToday, dateEndPlacement);

As @Mosho notes, you can also subtract the dates directly, without using getTime.

正如@Mosho 所指出的,您也可以直接减去日期,而无需使用getTime.

回答by Oleg

You should convert dateTodayand dateEndPlacementto Datetype. Please read Converting string to date in js

您应该将dateTodaydateEndPlacement转换为Date类型。请阅读在 js 中将字符串转换为日期

回答by Mosho

subtract dates (which, unformatted, are the number of seconds elapsed since 1 January 1970 00:00:00) , divide by 604,800,000 (milliseconds per week).

减去日期(未格式化,是自 1970 年 1 月 1 日 00:00:00 以来经过的秒数),除以 604,800,000(每周毫秒)。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

回答by Balakrishnan Bsk

You can use moment itself have all the functionality I mentioned the below code which could work perfectly

您可以使用 moment 本身具有我提到的以下代码的所有功能,这些功能可以完美运行

    var a = moment(a, 'DD-MM-YYYY');
    var b = moment(b, 'DD-MM-YYYY');
    days=b.diff(a, 'week');

don't forget to use moment js CDN

不要忘记使用moment js CDN