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

188
Views
Getting time in PM or AM from a datetime string

I have a dateTime string: "2022-02-03T18:40:00.000Z" I want to convert this into 6:40 PM. How would I go about doing something like that?

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

0

I would usually recommend using a date library for date manipulation and formatting, but this can be done quite easily without one as well.

const date = new Date("2022-02-03T18:40:00.000Z");

const convert = (date) => {
  const str = (date.getHours() < 12 || date.getHours() === 24) ? "AM" : "PM";
  const hours = date.getHours() % 12 || 12;
  return `${hours}:${date.getMinutes()}${str}`;
}


console.log(convert(date));

or using toLocaleString with en-us formatting.

const date = new Date("2022-02-03T18:40:00.000Z");

const convert = (date) => {
   return date.toLocaleTimeString(
    'en-US',
    {timeZone: 'UTC', hour12: true, hour: 'numeric', minute: 'numeric'}
  );
}

console.log(convert(date));

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!