Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

json date and javascript date and finding the difference in days

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading