1

I want to compare the date I receive from an API to the current date and if it exceeds 14 days. The date I receive is in this format.

"date": "2018-08-07T14:17:24+02:00"
chazsolo
  • 7,873
  • 1
  • 20
  • 44
develop05
  • 487
  • 1
  • 9
  • 23
  • 1
    Why not using momentjs? – Almaju Sep 07 '18 at 12:06
  • Is there a question? Something you've tried that's not working as expected? – chazsolo Sep 07 '18 at 12:07
  • @chazsolo The question is how to do this actually. I don't know how to like make the comparison between the days and months and years, it's a little confusing for me. – develop05 Sep 07 '18 at 12:10
  • Should the time be considered? E.g. is 2018-08-07T14:17:24+02:00 to 2018-08-08T00:00:00+02:00 one day? or do you want 24 hour periods? And what about crossing daylight saving boundaries? The daylight saving rules of the host will affect the result, not those of wherever the date came from. – RobG Sep 07 '18 at 13:16

3 Answers3

1

You can use the library date-fns to calculate this too. It has a smaller bundle size than Moment.

function exceedsDays(date, numberOfDays) {
  var today = dateFns.startOfToday();
  var diff = dateFns.differenceInDays(today, dateFns.parse(date));
  return diff > numberOfDays;
}

var date = "2018-08-07T14:17:24+02:00";
var result = exceedsDays(date, 14);

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.29.0/date_fns.min.js"></script>
lifeofmle
  • 332
  • 2
  • 11
0

let dateFrom = new Date("2018-08-07T14:17:24+02:00").getTime();
let today = new Date().getTime();
let days14 = 1000 * 60 * 60 * 24 * 14;

if(today - dateFrom > days14){  }
Anarion
  • 1,048
  • 6
  • 16
  • 1
    It's not that simple, as not all days are 24 hours long where daylight saving is observed. However, if the OP simply wants to deal with dates rather than exact hours, minutes and seconds, setting hours to zero and rounding should fix that. ;-) – RobG Sep 07 '18 at 13:14
0

If you go with momentjs you can do something like that. This will return you a boolean. You can reuse later this function to maybe check if more than 30 days etc. Just need to change the second argument. The first one is your date you want to check. By default moment() return now, this is the reason we don't need to create a date for now.

const oldDate = '2018-08-07T14:17:24+02:00';

function exceedNumOfDays(date, numOfDays) {
  return moment().diff(new Date(date), 'days') > numOfDays;
}

exceedNumOfDays(oldDate, 14)

I put the code on codesandbox, you can see the console at the bottom left. https://codesandbox.io/s/oo967v83xq

EQuimper
  • 5,811
  • 8
  • 29
  • 42
  • Thank you, your code seems to be right but what if I want to return true only if the difference between the current date and the received date is less than or equal 14 days? – develop05 Sep 07 '18 at 12:29
  • You can create same kind of function for those. Why I like this way it's because you abstract the library logic to match your. So if you change lib for like date-fns you just change the implementation of the function. – EQuimper Sep 07 '18 at 14:17