cs-Cz语言环境和JavaScript新的Date构造函数不允许使用简写日期 [英] cs-Cz locale and JavaScript new Date Constructor doesn't allow for shorthand date

查看:78
本文介绍了cs-Cz语言环境和JavaScript新的Date构造函数不允许使用简写日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试遵循MDN此处有关格式设置日期选项的文档

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

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat

对于cs-cz语言环境,我希望该月份为短"字元,手写日期,例如

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

<代码> 19.12. 2012 ,而不是 19.亲 201

此代码适用于en-us或it-it(意大利)等语言环境.

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));

打印出 2012年12月19日

任何人都可以帮助我使cs-cz语言环境正常工作.这段代码

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));

打印出

<代码> 19.12. 2012 ,而不是 19.pro 201

我想要 19.pro 201 使用JavaScript日期函数.在基于13个语言环境的代码中,这都是动态的,捷克语是唯一给我带来麻烦的代码.

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.

推荐答案

肯定是一个奇怪的难题.事实证明,如果在选项对象中包括 week 格式,它将提供您想要的输出.因此,下面的代码简单地使用了该代码,然后从字符串中删除了星期名称.

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);

这篇关于cs-Cz语言环境和JavaScript新的Date构造函数不允许使用简写日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆