trying to get a list of months for populating into...
# suitescript
r
trying to get a list of months for populating into a Suitelet form. Originally tried the following, but it looks like NetSuite has done something to native
Date()
object because instead of returning month name, it's just returning a
YYYY-MM-DD
output.
Copy code
for (let i = 1; i <= 12; i++) {
            monthField.addSelectOption({
                value: i,
                text: new Date(`${i}/1`).toLocaleDateString('en-US', {month: 'long'}),
                isSelected: i === currentMonth,
            });
        }
in debugger console
Copy code
new Date('1/1')
Date 2001-01-01T08:00:00.000Z
new Date('1/1').toLocaleDateString('en-US', { month: 'long' })
"2001-01-01"
1
b
they are actually following the spec of Date.prototype.toLocaleString
there is no support for
Intl.DateTimeFormat
, or Intl related apis in general
r
that I was aware of. Looking at polyfill1ing (not from polyfill.io) Intl support into my scripts. What I didn't know was the spec that if Intl isn't present, then it falls back to just Y-M-d
e
I know not everyone likes to use third-parties, but
moment
is my go-to lib for dates, and this is as simple as
moment.months()
which gives you locale-specific month names
this 1
r
I actually was thinking of moment, but seeing as it's depreciated in favor of more modern standards, was looking for a modern alternative, but might have to fall back to it given NetSuite's own implementation of JS, just to make sure all the expected functionality is there
we are US based, but was hoping I could include some language support to Month names. (yes, I know I can just hardcode all 12 month names into an array, but was trying to at least keep it locale based)
e
In a production JS app, I'd move away from moment for one of its successors instantly. In NetSuite, none of the successors work (the last time I tried), but moment still does.
👍 1
s
i've used luxon in a couple projects with success but i still use moment too so ¯\_(ツ)_/¯
👀 1
r
I was actually looking at Luxon, but it read to me like it inherits browser's own
Intl
library, which when running inside SuiteScript wouldn't work. I could be wrong though.
b
the new libraries use the Intl related apis, so there is a lot of missing functionality
r
yea... what I ran into as well. I start down a rabbit hole of trying to shim/polyfill Intl, but haven't gotten anywhere yet
s
yeah, exactly.
b
you have to polyfill Intl related apis using something like https://github.com/formatjs/date-time-format-timezone
👍 1
or you can go with the easiness of using moment-timezone, which includes the timezone related information
👍 1
r
thanks. including moment.js is my current fallback, so at least I'll have something