Home >
Technical & Creative Skills >
Programming, Servers & Scripts
Different approach to dates in JavaScript (2)
09-19-2020 07:36 PM
#1
plutus (Member)
Different approach to dates in JavaScript
I found so many landers with this strange approach of setting up months/weekdays arrays and then accessing it based on current date, this works but is not 100% accurate, I just found out that TH for example is 543 year ahead of us -
https://en.wikipedia.org/wiki/Date_and_time_notation_in_Thailand
Here is a one-liner that displays full date using given locale (th-TH in that case but this can be replaced with URL parameter, see below)
Code:
(new Date()).toLocaleDateString('th-TH', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })
URL-parameter version:
Code:
(new Date()).toLocaleDateString(getURLParameter('locale'), { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })
and usage:
Code:
https://some-lander.com/?locale=es
in case you would need only weekday name you can use it like this
Code:
(new Date()).toLocaleDateString('de', { weekday: 'long'})
List of accepted locales:
https://github.com/libyal/libfwnt/wi...ge-identifiers
https://github.com/libyal/libfwnt/wi...x0400---0x04ff
09-19-2020 10:53 PM
#2
jeremie (Moderator)

Originally Posted by
plutus
I found so many landers with this strange approach of setting up months/weekdays arrays and then accessing it based on current date, this works but is not 100% accurate, I just found out that TH for example is
543 year ahead of us -
https://en.wikipedia.org/wiki/Date_and_time_notation_in_Thailand
Here is a one-liner that displays full date using given locale (th-TH in that case but this can be replaced with URL parameter, see below)
Code:
(new Date()).toLocaleDateString('th-TH', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })
URL-parameter version:
Code:
(new Date()).toLocaleDateString(getURLParameter('locale'), { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })
and usage:
Code:
https://some-lander.com/?locale=es
in case you would need only weekday name you can use it like this
Code:
(new Date()).toLocaleDateString('de', { weekday: 'long'})
List of accepted locales:
https://github.com/libyal/libfwnt/wi...ge-identifiers
https://github.com/libyal/libfwnt/wi...x0400---0x04ff
Hey,
This is not a strange approach. This was the only working approach back 3-5 years ago if you wanted to have a script working with almost all browsers as tolocalestring did not exist at first, and then was not supported by lots of browsers back then.
I have given some code snippets here with tolocalestring here too:
https://stmforum.com/forum/showthrea...l=1#post402419
This is method is still not 100% supported, although with 96/98%, it is way better than a few years ago.
Home >
Technical & Creative Skills >
Programming, Servers & Scripts