cs-Cz locale and JavaScript new Date Constructor doesn't allow for shorthand date

喜你入骨 提交于 2021-01-29 12:38:21

问题


So I'm trying to follow the docs here on MDN regarding the date options for formatting

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat

For the cs-cz locale, I want the month to be in "short" hand form when writing a date such as

19. 12. 2012 instead of 19. pro 201

This code works for locales such as en-us or it-it (italy) and others.

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0, 200));
var options = { month: 'short', day: 'numeric', year: 'numeric'};
console.log(new Intl.DateTimeFormat('en-us', options).format(date));

Prints out Dec 19, 2012

Can anyone help me with getting the cs-cz locale to work. This code

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0, 200));
var options = { month: 'short', day: 'numeric', year: 'numeric'};
console.log(new Intl.DateTimeFormat('cs-cz', options).format(date));

prints out

19. 12. 2012 instead of 19. pro 201

I'd like 19. pro 201 using the JavaScript Date Function. This is all dynamic in my code based on 13 locales and czech is the only one giving me trouble.


回答1:


A weird conundrum for sure. As it turns out, if you include the week format in the options object it provides the output you seek. So the code below simple uses that and then removes the week name from the string.

I did not modify the year, and will leave that up to you, but I do believe this solves your problem and answers your question.

const options = { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric' };
const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0, 200));
const out = date.toLocaleDateString('cs-CZ', options).split(' ').splice(1).join(' ');
console.log(out);


来源:https://stackoverflow.com/questions/64506088/cs-cz-locale-and-javascript-new-date-constructor-doesnt-allow-for-shorthand-dat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!