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 away
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);
>Solution :
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);