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

163
Views
how to compare date and time in javascript using moment

I am trying to compare updated date and time with current date and time. here i am getting one array of object there inside i have one updated at key . I need to find which object is near to current date and time and update another key inside that object.

     for(var i =0;i<data.length;i++){ 
        if(data[i].updated_at > data[i+1].updated_at ){
          data.is_latest="true"
              }
       }
 // below is my data
  
                              [
                            {
                                "is_latest": "",
                                "created_at": "2021-09-21T21:24:05.000Z",
                                "updated_at": "2021-09-21T17:53:29.000Z"
                            },
                            {
                                "is_latest": "",
                                "created_at": "2021-09-29T21:24:05.000Z",
                                "updated_at": "2021-09-29T17:53:29.000Z"
                            }
                        ]

  // in the above i am getting data here how to find updated_at nearer to current date and time using moment.js
      
                                 [
                            {
                                "is_latest": "",
                                "created_at": "2021-09-21T21:24:05.000Z",
                                "updated_at": "2021-09-21T17:53:29.000Z"
                            },
                            {
                                "is_latest": true,
                                "created_at": "2021-09-29T21:24:05.000Z",
                                "updated_at": "2021-09-29T17:53:29.000Z"
                            }
                        ]

how to resolve this using map function

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can try:

let today = Date.now();
let latest_index = 0, latest_diff = today - (new Date(data[0].updated_at)).getTime();

for(let i=1;i<data.length; i++){ 
  let updated_date = (new Date(data[i].updated_at)).getTime();

  if (today - updated_date < latest_diff){
    latest_index = i;
    latest_diff = today - updated_date;
  }
     
}

Note:

(new Date()).getTime;
// This will return you the number of milliseconds elapsed from January 1, 1970 
// if your date is less than that date, the value will be negative
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!