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

168
Views
How to parse dates in javascript?

How can I parse dates coming in this example format 7 July 2021 at 1:36:23 AM Z in pure javascript? Tried a lot of things even with moment.js but was not fruitful. I presume the at in the middle of the date is the problem. Any help would be appreciated. Thanks in advance

Edit :

Tried the replace() function as mentioned in the comments, It worked perfectly for chrome, but safari is throwing Invalid Date error.

Sample code:

function convertUTCDateToLocalDate(date) {
    let newDate = new Date(date);
    let options = {
        year: "numeric",
        month: "long",
        day: "2-digit",
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
    };
    return newDate.toLocaleDateString("en", options);
}
let dateString = "7 July 2021 at 1:36:23 AM Z"
console.log(convertUTCDateToLocalDate(dateString.replace('at', ''));

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

0

A simple parser, assuming the offset is always Z:

// Parse timestamp in format "7 July 2021 at 1:36:23 AM Z"
function parseTS(ts) {
  ts = ts.toLowerCase();
  let months = ['jan','feb','mar','apr','may','jun',
                'jul','aug','sep','oct','nov','dec'];
  let [D, M, Y, x, h, m, s, ap, os] = ts.split(/\W/);
  return new Date(Date.UTC(Y, months.indexOf(M.substr(0,3)), D,
    h%12 + (ap == 'pm'? 12 : 0), m, s)); 
}

['7 July 2021 at 1:36:23 AM Z',
 '7 July 2021 at 1:36:23 PM Z'
].forEach(ts => console.log(`${ts} : ${parseTS(ts).toISOString()}`));

If you want to use moment.js, you should always provide the format using tokens as for formatting unless it's a supported format. The "at" can be elided with square brackets:

// 7 July 2021 at 1:36:23 AM Z
let d = moment('7 July 2021 at 1:36:23 AM Z', 'D MMMM, YYYY [at] h:mm:ss a Z');

console.log(d.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

You can use replace regex dateString.replace(/at|Z/g, '') for Safari, it worked for Safari and Chrome

about 4 years ago · Juan Pablo Isaza Report

0

The best option would be to request an ISO 8601 formatted date-time from wherever your string is coming from. Maybe there is an API option that allows you to pass the format. If you are using some custom input there might be an option to retrieve an ISO 8601 formatted string as well.


If for some reason you cannot change the string at its source you could always manually converted in into the correct format.

const months = {
  january: 1,
  february: 2,
  march: 3,
  april: 4,
  may: 5,
  june: 6,
  july: 7,
  august: 8,
  september: 9,
  october: 10,
  november: 11,
  december: 12,
};
const meridiems = {
  am: 0,
  pm: 12,
};

const input = "7 July 2021 at 1:36:23 AM Z";
let [day, month, year, _at, time, meridiem, offset] = input.split(" ");
let [hour, minute, second] = time.split(":");

day = day.padStart(2, "0");

month = months[month.toLowerCase()];
month = month.toString().padStart(2, "0");

hour = hour % 12 + meridiems[meridiem.toLowerCase()];
hour = hour.toString().padStart(2, "0");

const iso8601 = `${year}-${month}-${day}T${hour}:${minute}:${second}${offset}`;

console.log(iso8601);
console.log(new Date(iso8601));

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!