-1

I have a date coming over from a database. The response is via django python (although that shouldn't matter since json is json) but context. I then get the current date in javascript. the goal is to take todays date and the one sent over by the server and find out how many days apart. example today and tomorrow is 1 day awayenter image description here

I also have a screen shot of the console.

What am I doing wrong.

const today = new Date();
     console.log('today');
     console.log(today);
     const creationdate = this.creatorobject.creationdate;
     console.log(creationdate);
     const dayssince = today - creationdate;
     console.log('here is days since');
     console.log(dayssince);

1 Answers1

0

First i would have a variable to convert the JSON obj date to date format by simply passing it as a string

var creationDate = new Date(this.creatorobject.creationdate);

ex : const d = new Date("2015-03-25");

then next find the difference between the 2 days ie: today - creationDate..

If they are 2 date formats then you get milliseconds. Convert milliSeconds to days and round it up to get a date.

var today = new Date();
var creationDate = new Date("2022-01-01");
var diff = today - creationDate;
const convertedDate = diff / (1000*60*60*24);
console.log(convertedDate);
  • *creationDate* is parsed as UTC, whereas `new Date()` is based on local time, so the difference may not be what the OP expects. See [*Difference between two dates in years, months, days in JavaScript*](https://stackoverflow.com/questions/17732897/difference-between-two-dates-in-years-months-days-in-javascript?r=SearchResults&s=1|191.0090). – RobG Jan 05 '22 at 05:14