1

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', ''));

bahdotsh
  • 459
  • 3
  • 17

3 Answers3

1

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>
RobG
  • 142,382
  • 31
  • 172
  • 209
0

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

zaha
  • 26
  • 1
  • 3
  • 3
    See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Dec 16 '21 at 11:38
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));
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
  • You can get all the parts in one go if split on `\W`. In *months*, june is misspelled "juny". In `parseInt(hour, 10) % 12` the use of *parseInt* is redunant, `hour % 12` is sufficient. Having parsed the string, don't then build another string that must be parsed by the built–in parser, give the parts directly to the constructor: `new Date(Date.UTC(year, month-1, day, hour, minute, second))`, which also avoids a lot of needless formatting. ;-) – RobG Dec 16 '21 at 11:12