简体   繁体   中英

Node.js dates comparison

In Node.js, how do I compare the date with today's date if one of the dates is a string?

var retdate = new Date();
retdate.setDate(retdate.getDate()-7);
var mydate = '2016-07-26T09:29:05.000Z'

How do I compare 'retdate' and 'mydate' so it returns date older than 7 days? I believe the string needs to be modified?

Updated solution:

Since OP is asking for a solution that is based on standard Javascript API.

What you could do is, since your date string is compliance to the ISO-8601 date format, you can create the Date object through passing the date string to the constructor.

Once you got 2 Date objects you can directly subtract from objects giving you the epoch datetime. So using that, you just need to dividend it by the total number of milliseconds in a week (7 days) to figure out whether Date A is older than Date B or not.

Example:

 var retdate = new Date(); retdate.setDate(retdate.getDate()-7); var mydatestring = '2016-07-26T09:29:05.00'; var mydate = new Date(mydatestring); var difference = retdate - mydate; // difference in milliseconds const TOTAL_MILLISECONDS_IN_A_WEEK = 1000 * 60 * 60 * 24 * 7; if (Math.floor(difference / TOTAL_MILLISECONDS_IN_A_WEEK) >= 7) { console.log("Current date is more than 7 days older than : " + mydatestring); }

MomentJS solution:

Like 'Newbee Dev' said MomentJS is a good JS datetime module for solving all kinds of date related problems.

First parse your datetime string using the moment(...) constructor then use the diff(...,'days') API to do the day comparison.

Example:

 var datetime = '2016-07-26T09:29:05.000Z'; var localTime = moment(); var otherTime = moment(datetime); console.log("Current datetime is older than " + datetime + " by 7 days = " + (localTime.diff(otherTime, 'days') >= 7));
 <script src="http://momentjs.com/downloads/moment.js"></script>

See : http://momentjs.com/

You can convert the date into milliseconds and do the math and convert it back to standard date.

 //Today's date const today = new Date() //Six days before today const sixDaysAgo = new Date(+(new Date()) - 6 * 24 * 60 * 60 * 1000) //Seven days before today const sevenDaysAgo = new Date(+(new Date()) - 7 * 24 * 60 * 60 * 1000) //One year ago const oneYearAgo = new Date(+(new Date()) - 365 * 24 * 60 * 60 * 1000) //Given date from a date string const givenDate = new Date("2016-07-26T09:29:05.000Z") //Convert the range of days to milliseconds //(This wont work if the date very old) const sevenDaysInMiliSec = 7 * 24 * 60 * 60 * 1000 var dateToValidate = sevenDaysAgo; if (today - dateToValidate >= sevenDaysInMiliSec) { console.log(dateToValidate + " is seven days older or more") } else { console.log(dateToValidate + " is less than seven days old") } dateToValidate = sixDaysAgo; if (today - dateToValidate >= sevenDaysInMiliSec) { console.log(dateToValidate + " is seven days older or more") } else { console.log(dateToValidate + " is less than seven days old") } dateToValidate = oneYearAgo; if (today - dateToValidate >= sevenDaysInMiliSec) { console.log(dateToValidate + " is seven days older or more") } else { console.log(dateToValidate + " is less than seven days old") } dateToValidate = givenDate; if (today - dateToValidate >= sevenDaysInMiliSec) { console.log(dateToValidate + " is seven days older or more") } else { console.log(dateToValidate + " is less than seven days old") }

use moment js just add this module into your dependencies

var moment = require('moment');

retdate = moment(retdate).format('YYYY-MM-DD');
mydate = moment(mydate).format('YYYY-MM-DD');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM