Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
Comparing a moment.js obect to a timestamp from JSON response

I am trying to compare a moment object (30 days ago) to a given timestamp. Both isBefore and isAfter are returning false. Im not sure where i'm going wrong?

var startdate = moment.utc().subtract(30,'days')

var RequestedDate = moment.utc('14/12/2021, 11:26')

var isbefore = startdate.isBefore(RequestedDate)
var isafter = startdate.isAfter(RequestedDate)

console.log(isafter)
console.log(isbefore)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The requested date is not in the right format, based on the error emitted in a fiddle of your code:

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.

If you're able to use the "Date + Format" constructor, then you can parse something like this:

var RequestedDate = moment.utc('14/12/2021, 11:26', 'D/M/Y, H:m', true)
// "Tue Dec 14 2021 11:26:00 GMT+0000"

Note the use of strict mode, as in the docs:

You may get unexpected results when parsing both date and time. The below example may not parse as you expect:

moment('24/12/2019 09:15:00', "DD MM YYYY hh:mm:ss");
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!