0

I want count the number of days between two date values in a string.

For example I have a text like this: 2019-08-01 to 2019-08-03. How can I get the number of days between the dates in that string? My idea so far is to explode the string to an array so I can get array 0 and array 2. However I have no idea how to implement this in JS/JQ.

I. Ahmed
  • 2,438
  • 1
  • 12
  • 29
Cross Vander
  • 2,077
  • 2
  • 19
  • 33
  • 1
    Is the string format always the same? As in, are the two date values always separated by `' to '`? If so you can split the string, then [get the difference between them](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript). It would be better to retrieve the dates directly, though, without having to hack around a string – Rory McCrossan Aug 13 '19 at 08:30
  • Yes, it always separate by text 'to' – Cross Vander Aug 13 '19 at 09:07

3 Answers3

4

Split the date string by to then take the first and second items from the created string array and convert them to dates then subtract the two.

var dateString = '2019-08-01 to 2019-08-03';
var dates = dateString.split(' to ');
var fromDate = Date.parse(dates[0]);
var toDate = Date.parse(dates[1]);
var difference = (toDate - fromDate) / (1000 * 60 * 60 * 24);
console.log(difference);

Dividing by (1000 * 60 * 60 * 24) converts the result from milliseconds to days.

Dumisani
  • 2,988
  • 1
  • 29
  • 40
1

You can use Moment.js to parse dates, it will accept most formats. You can then use the .diff function to get the difference in whichever time period you wish.

We'll first split the string using whichever RegEx pattern we consider most appropriate.

The Moment.js parser ignores non-alphanumeric characters, so both of the following will return the same thing:

moment("12-25-1995", "MM-DD-YYYY");
moment("12/25/1995", "MM-DD-YYYY");

This gives a degree of flexibility with parsing.

dateString = '2019-08-01 to 2019-08-03';
dates = dateString.split(/\s\w+\s/);

date1 = new moment(dates[0], 'YYYY-MM-DD');
date2 = new moment(dates[1], 'YYYY-MM-DD');
differenceDays = date2.diff(date1, 'days');

console.log('Difference in days: ', differenceDays);

// This will also work with other separators
dateString = '2019/08/01 bis 2019.08.03';
dates = dateString.split(/\s\w+\s/);

date1 = new moment(dates[0], 'YYYY-MM-DD');
date2 = new moment(dates[1], 'YYYY-MM-DD');
differenceDays = date2.diff(date1, 'days');

console.log('Difference in days (different separators): ', differenceDays);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
-2
var today = new Date();  // keep today's date
var difference = your_date_variable - today;  // difference - will have the number of days.
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Apparao
  • 1,634
  • 1
  • 12
  • 17
  • 1
    The OP wants the difference between the two dates in the string. Your answer makes no attempt to retrieve those figures, and additionally compares to the wrong date. – Rory McCrossan Aug 13 '19 at 08:48
  • And additionally, subtracting two date objects returns the difference in milliseconds, not the number of days. – JJJ Aug 13 '19 at 09:39