1

I have a small code that picks up the dates from a json file. and returns the amount of days left before it expires.

How ever its returning NaN in console log.

var start = "2019/03/12";
var end = "2020/03/12";
days = (end- start) / (1000 * 60 * 60 * 24);
console.log(Math.round(days));

this should be correct. but its not working.

Mic
  • 331
  • 1
  • 2
  • 14
  • Possible duplicate of [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – Liam Mar 13 '19 at 16:07

4 Answers4

1

You need to change end and start to Date

var start = "2019/03/12";
var end = "2020/03/12";
days = ( new Date(end)- new Date(start) ) / (1000 * 60 * 60 * 24);
console.log(Math.round(days));
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • thank you so much. i will mark as correct in 13 minutes. – Mic Mar 13 '19 at 16:03
  • 2
    Bear in mind that this won't work everywhere. The parsing of the **string** `"2020/03/12"` to the date you expect will depend on the location of the user. i.e. is this 12th march of the 3rd of December? A better solution to this is to use moment.js – Liam Mar 13 '19 at 16:05
  • 3
    To add to @Liam 's comment, you can combat that with [toLocaleString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString). – Lewis Mar 13 '19 at 16:07
0

Try this...using Date objects

var start = new Date("2019/03/12");
var end = new Date("2020/03/12");
days = (end - start) / (1000 * 60 * 60 * 24);
console.log(Math.round(days));
Lazy Coder
  • 1,157
  • 1
  • 8
  • 18
0

you have to convert your string date into a javascript date but overall i would recommend to use moment as javascript dates can be a pain

to convert your string into a javascript datetype you can do it like this

var mydate = new Date('2011-04-11T10:20:30Z'); // <--- you have to format it

approach 2)

new Date('2011', '04' - 1, '11', '11', '51', '00')

if you want to use moment, you can do it like this:

var mydate = moment("2014-02-27T10:00:00").format('DD-MM-YYYY'); // <-- here inside the format function you can define how your string get's parsed
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
xzesstence
  • 679
  • 6
  • 19
  • Moment is great, however for this particular example it's not required. The other answers are simple enough. – Lewis Mar 13 '19 at 16:08
  • @Liam toLocaleString() will fix that without having to load an external library. Don't get me wrong, I love moment and would probably use it myself for something like this. – Lewis Mar 13 '19 at 16:10
  • yeah thats true, moment is awesome, i usually use it for time calculations but of course you can always go down to the timestamp and calculate with it if you have a javascript date. best functions for date calculating is dotnet with its timespan and datetime types, moment should improve into that direction and javascript should include moment in its syntax by default – xzesstence Mar 13 '19 at 16:13
  • `you have to format it` this works because it is a [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601) date string which **always** works – Liam Mar 13 '19 at 16:13
  • you are right in this case @Liam but i wanted to point out in general that it needs a specific format to work – xzesstence Mar 13 '19 at 16:16
-1

Use diff function of moment.js. But you have to format it before use.

const format = date => date.replace(/\//g, '-') 

var start = moment(format("2019/03/12"));
var end = moment(format("2020/03/12"));

console.log(end.diff(start, 'days'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
bird
  • 1,872
  • 1
  • 15
  • 32