有没有可以根据当前语言环境给我日期格式的函数

时间:2020-03-23 14:01:57

标签: javascript internationalization

我有toLocaleDateString,它根据当前语言环境来格式化日期。是否有功能可以根据当前语言环境以MM / DD / YYYY格式返回格式?我希望答案基于语言环境像“ MM / DD / YYYY”或“ DD / MM / YYYY”。

const options = { day: '2-digit', month: '2-digit', year: 'numeric' }
{selectedDate.toLocaleDateString(locale, options)}

根据区域设置以正确的格式为我提供日期

是否存在

{selectedDate.toLocaleDateFormat(locale, options)}

将返回DD / MM / YYYY或MM / DD / YYYY或任何语言环境的格式 我的密码框有密码

https://codesandbox.io/s/musing-benz-yzvhx

2 个答案:

答案 0 :(得分:0)

没有官方的API可以提供给您。

答案 1 :(得分:-1)

var d = new Date();
d = String(d)


var res = d.split("-");

console.log(res)

preFormat = (res[0].split(" "))

month = preFormat[1]
day = preFormat[2]
year = preFormat[3]

UsFormat = ("JanFebMarAprMayJunJulAugSepOctNovDec".indexOf(month) / 3 + 1) + '/' + day + '/' + year


UkFormat = day + '/' + ("JanFebMarAprMayJunJulAugSepOctNovDec".indexOf(month) / 3 + 1) + '/' + year
console.log(UsFormat)
console.log(UkFormat)