-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove dependency on Moment.js #309
Comments
In one instance within JS's built-in date formatting can likely extract this value. String parsing will exacerbate an existing problem of relying on Locale, so this might be a good opportunity to adapt the code to not be hard-coded with English month names. |
One way to get the month abbreviation:
|
This could probably be optimized but is a clear way to get an array of short month names: function getMonthNames() {
const months = []
for (let m=0; m<12; m++) {
months.push((new Date(2020, m)).toLocaleDateString('default', {month: 'short'}))
}
return months
}
console.log(getMonthNames())
> [
'Jan', 'Feb', 'Mar',
'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'
] |
https://tools.ietf.org/html/rfc7231#section-7.1.1.1 specifies HTTP dates should be of the form "Jan", "Feb", "Nov", etc., so en-us (or equivalent) should be set for the locale string instead of relying on the browser/client's locale, cf. function getMonthNames() {
const months = []
for (let m=0; m<12; m++) {
months.push((new Date(2020, m)).toLocaleDateString('ar-EG', {month: 'short'}))
}
return months
}
console.log(getMonthNames())
> [
'يناير', 'فبراير',
'مارس', 'أبريل',
'مايو', 'يونيو',
'يوليو', 'أغسطس',
'سبتمبر', 'أكتوبر',
'نوفمبر', 'ديسمبر'
] |
If you are talking about |
Slightly more succinct: function getMonthNames() {
const months = []
Array.from({length: 12}, (_, m) =>
months.push((new Date(2020, m)).toLocaleDateString('en-us', {month: 'short'}))
)
return months
} @ibnesayeed, I figured that but wanted to be sure, so provided the above reference. |
function getMonths(locale, format){
const formatter = new Intl.DateTimeFormat(locale, {month: format})
return [...Array(12).keys()].map(m => formatter.format(new Date(2020, m)))
}
getMonths('ar', 'long')
// ["يناير", "فبراير", "مارس", "أبريل", "مايو", "يونيو", "يوليو", "أغسطس", "سبتمبر", "أكتوبر", "نوفمبر", "ديسمبر"]
getMonths('en', 'long')
// ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
getMonths('en', 'short')
// ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
getMonths('es', 'short')
// ["ene.", "feb.", "mar.", "abr.", "may.", "jun.", "jul.", "ago.", "sept.", "oct.", "nov.", "dic."]
getMonths('hi', 'short')
// ["जन॰", "फ़र॰", "मार्च", "अप्रैल", "मई", "जून", "जुल॰", "अग॰", "सित॰", "अक्तू॰", "नव॰", "दिस॰"] |
Here is a one-liner function definition: const getMonths = (locale, format) => [...Array(12).keys()].map(m => (new Date(2020, m).toLocaleDateString(locale, {month: format})))
getMonths3('es', 'short')
// ["ene.", "feb.", "mar.", "abr.", "may.", "jun.", "jul.", "ago.", "sept.", "oct.", "nov.", "dic."] |
@ibnesayeed Thanks for the revision. I was testing with the spread operator in Node and was getting odd syntactic results. Using |
That's why I made it a parameterized function, you can call the function twice to create a list of months in the specific locale for use in the UI and one with |
Co-authored-by: Sawood Alam <ibnesayeed@gmail.com>
@ibnesayeed Thanks! Your contribution was noted in 3537420. |
Adapting the UI to the user's locale should be a different issue but your function will be useful there (#311). This issue is to work toward removing moment.js, for which month name comparison is related. |
The project has moved to be a legacy project and the maintainer recommend to replace it. Other relevant literature (from source):
The source also recommends some alternatives (e.g., Luxon, Day.js, date-fns).
This issue will track the degree of coupling with the dependency.
The text was updated successfully, but these errors were encountered: