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

267
Views
JavaScript Date constructor returns invalid date in CMS (server-side JS)

I'm making an React app where I query Microsoft Graph API where I get the response date 2021-11-03T07:45:00.0000000.

When I try to convert it to a JavaScript Date like this it gives me "invalid date" on let date = new Date(dateString);

    let dateString = "2021-11-03T07:45:00.0000000"
    let date = new Date(dateString);
    let timezoneOffset = date.getTimezoneOffset();
    date.setMinutes(date.getMinutes() - timezoneOffset);
    let hours = date.getHours();
    let minutes = date.getMinutes();
    let formattedMinutes = minutes > 9 ? minutes : '0' + minutes;  
    console.log(hours + ':' + formattedMinutes); 

This works in normal JS but the problem is that I use a CMS (server-side JS) that doesn't support new Date(dateString) so is it possible to format the date value to 8:45 without using Date constructor in JS? (npm libraries work in the CMS so if you have any solution with third party libraries it's fine.)

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

0

A regex could help in this situation. This is a simple example when there is no timezone offset.

str = '2021-11-03T07:45:00.0000000'

const time = str.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}T([0-9:]{5})/)[1]

const timeFormatted = time.split(':').map(num => parseInt(num)).reduce((timeFormatted, num) => {
  if (timeFormatted) timeFormatted += ':'
  timeFormatted += num
  return timeFormatted
})

console.log(timeFormatted)

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!