1

Sorry if this has been asked before, but I haven't been able to find anything. Here's essentially what I'm trying to do:

new Date(response.departureDate).getTime() - new Date(response.arrivalDate).getTime()

I need to calculate the total number of days (will always be a whole integer) between an arrival and departure date. These dates are strings, structured as 'YYYY-MM-DD'.

How do I go about this?

j_d
  • 2,818
  • 9
  • 50
  • 91

5 Answers5

1

You can use regexp OR a much simpler approach would be to become familiar with MomentJS API that lets you deal with dates in JS very smoothly (works in Node and browser)

http://momentjs.com/

It does add another tool to your toolbox, but as soon as you are manipulating dates, it is definetely worth it IMHO.

Way to go with MomentJS :

var depDate = moment(response.departureDate);
var arrDate = moment(response.arrivalDate);
var nbDays = depDate.diff(arrDate, 'days');
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
kartsims
  • 1,001
  • 9
  • 16
1

Look at the Miles' answer here Just change it to:

function parseDate(str) {
var mdy = str.split('-')
return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function daydiff(first, second) {
    return Math.round((second-first)/(1000*60*60*24));
}

and use:

daydiff(parseDate(response.departureDate), parseDate(response.arrivalDate));
Community
  • 1
  • 1
Cem Şengezer
  • 213
  • 1
  • 12
0

You can use RegEx to change them.

new Date(response.departureDate.replace(/-/g, "/")).getTime()
    - new Date(response.arrivalDate.replace(/-/g, "/")).getTime()

So the RegEx .replace(/-/g, "/") will replace all the - to /, and JavaScript will be able to read it right.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

I hope this example help for you

Apart from .diff(), you could also use moment durations: http://momentjs.com/docs/#/durations/

Example Fiddle: https://jsfiddle.net/abhitalks/md4jte5d/

Example Snippet:

$("#btn").on('click', function(e) {
    var fromDate = $('#fromDate').val(), 
        toDate = $('#toDate').val(), 
        from, to, druation;

    from = moment(fromDate, 'YYYY-MM-DD'); // format in which you have the date
    to = moment(toDate, 'YYYY-MM-DD');     // format in which you have the date

    /* using duration */
    duration = moment.duration(to.diff(from)).days(); // you may use duration

    /* using diff */
    //duration = to.diff(from, 'days')     // alternatively you may use diff

    /* show the result */
    $('#result').text(duration + ' days');

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>
From: <input id="fromDate" type='date' />
To: <input id="toDate" type='date' />&nbsp;&nbsp;
<button id="btn">Submit</button><hr />
<p id="result"></p>
Bhadresh Shiroya
  • 261
  • 3
  • 13
0

You can use something like this

function countDays(date1, date2)
{
    var one_day=1000*60*60*24;
    return Math.ceil((date1.getTime()- date2.getTime()) /one_day);
}

countDays(new Date(response.departureDate), new Date(response.arrivalDate));
S. Nadezhnyy
  • 582
  • 2
  • 6
  • NO, do not parse strings with the Date constructor (or Date.parse), it is very unreliable. Manually parse Date strings (2 lines of code or use a library). – RobG Mar 15 '16 at 03:05
  • I think that depends on if you can trust response : looks like it is OK for John Doe, so I don't mind ) – S. Nadezhnyy Mar 15 '16 at 08:19
  • The OP is asking for advice. The format 'YYYY-MM-DD' (ISO 8601 date without timezone) will be parsed by ECMAScript 2015 compliant browsers as UTC, which will surprise those expecting it to be parsed as local. It will not be parsed by IE 8, which is still has about 8% usage (which is more than all versions of desktop Safari combined). Certain versions of some browsers will parse it as local. So even if the format is guaranteed, appropriate parsing isn't. – RobG Mar 15 '16 at 08:38