RangeError: Format string contains an unescaped latin alphabet character `z`

See original GitHub issue

I’m getting this RangeError when trying to parse a date string like this 2020-11-09 6:05pm GMT using z for the timezone as noted in the unicode date symbol field table.

Similar to https://github.com/date-fns/date-fns/issues/1473 except this is about parsing rather than formatting.

I realize that the docs for parse don’t list z as a pattern but using x or X or xx etc doesn’t work as well. As an aside, the docs seem to imply that the functions support all of the patterns in the unicode standard – plus additions, but that doesn’t seem to be the case.

I created a sandbox to demonstrate the specific error here: https://codesandbox.io/s/date-fns-parsing-issue-qkyht

Is this format not possible to parse?

Thanks ahead of time for the help!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
fturmelcommented, Jan 26, 2022

@scscgit I like the idea of a page to compare token compatibility between the parse and format functions, making note of it.

format properly recognizes the z without any issue

Actually, it looks like z in format was implemented to behave like O… so that’s going to be a problem for interop and a breaking change if it gets fixed.

import { format } from "date-fns";

const date = new Date(2021, 0, 1);
console.log(format(date, "z")); // GMT-5
console.log(format(date, "zzzz")); // GMT-05:00

https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table

Screen Shot 2022-01-26 at 3 26 57 PM
1reaction
fturmelcommented, Jan 26, 2022

@vadimpopa Glad you figured out an approach, and yes I imagine this would get very unwieldy for several languages.

Little bonus code snippet update that will scale much better if a larger list of offsets is needed.

import { parse } from "date-fns";
import { de } from "date-fns/locale";

const input = "7/22/2021 5:41:26 vorm. OESZ";

const offsets = {
  MESZ: "+0200",
  OESZ: "+0300"
};

const abrv = input.match(/ ([A-Z]+)$/)[1];
const offset = offsets[abrv];
const inputWithOffset = input.replace(abrv, offset);

const res = parse(inputWithOffset, "M/d/yyyy h:mm:ss a xx", new Date(), {
  locale: de
});

console.log(res.toISOString()); // 2021-07-22T02:41:26.000Z

And a few resources: https://www.timeanddate.com/time/zones/ https://en.wikipedia.org/wiki/List_of_tz_database_time_zones https://www.iana.org/time-zones

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't resolve RangeError: Format string contains an ... - GitHub
I found if I use @date-io/date-fns2.x , there will truely occur the problem 'Can't resolve RangeError: Format string contains an unescaped latin ......
Read more >
Cannot get material-ui datepicker to work - Stack Overflow
Uncaught RangeError: Format string contains an unescaped latin alphabet character n. you have to downgrade to @date-io@^1.3.13.
Read more >
date-fns@2.29.3 - runpkg
The characters in the format string wrapped between two single quotes ... @throws {RangeError} format string contains an unescaped latin alphabet character.
Read more >
Format string contains an unescaped latin alphabet character `z`
I'm getting this RangeError when trying to parse a date string like this 2020-11-09 6:05pm GMT using z for the timezone as noted...
Read more >
Format string contains an unescaped latin alphabet character `n`
RangeError : Format string contains an unescaped latin alphabet character `n`. Comment. 3.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found