13

I'm trying to return the number of weeks between two dates using 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.

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'

Any ideas what I am doing wrong?

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

 calculateWeeksBetween(dateToday, dateEndPlacement);
Cameron
  • 27,963
  • 100
  • 281
  • 483

6 Answers6

24

I would recommend using moment.js for this kind of thing.

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

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));
Jack Allan
  • 14,554
  • 11
  • 45
  • 57
5

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

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.

Kara Brightwell
  • 2,529
  • 1
  • 21
  • 29
3

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

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

live2
  • 3,771
  • 2
  • 37
  • 46
Balakrishnan Bsk
  • 474
  • 2
  • 5
  • 17
3

If you need actual weeks between to dates, and not the number of seven days between them:

const week = 7 * 24 * 60 * 60 * 1000;
const day = 24 * 60 * 60 * 1000;

function startOfWeek(dt) {
    const weekday = dt.getDay();
    return new Date(dt.getTime() - Math.abs(0 - weekday) * day);
}

function weeksBetween(d1, d2) {
    return Math.ceil((startOfWeek(d2) - startOfWeek(d1)) / week);
}
localhost
  • 845
  • 3
  • 14
  • 32
1

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).

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

Mosho
  • 7,099
  • 3
  • 34
  • 51
1

You should convert dateToday and dateEndPlacement to Date type. Please read Converting string to date in js

Community
  • 1
  • 1
Oleg
  • 67
  • 2