使用moment.js,如何显示用户的当前日期格式? [英] Using moment.js, how to display the current date format for the user?

查看:279
本文介绍了使用moment.js,如何显示用户的当前日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个文本字段,我想要一个合适的占位符。典型的占位符将类似于:mm / dd / yyyy。

Given a text field, I want to have a suitable placeholder. A typical placeholder will be something like: "mm/dd/yyyy".

但是,我想使用moment.js来使用区域设置感知日期。

However, I would like to use locale-aware dates using moment.js.

这意味着我将指定l作为moment.js日期格式,在这种情况下,我如何确定moment.js将使用的日期格式?

This means that I will be specifying "l" as the moment.js date format, howe do I determine the date format that moment.js will be using in this case?

用户不会理解l的含义,因此在占位符文本中使用此值几乎没有意义。

The user will not understand what "l" means, so using this value in the placeholder text makes very little sense.

具体来说,我希望能够访问像moment的内部defaultLongDateFormat这样的东西。 (虽然这只是一个默认 - moment.js可能会更新它,或者在运行时为区域设置感知日期格式有一些其他映射 - 我想访问该映射。)

Specifically, I am hoping to be able to access something like moment's internal "defaultLongDateFormat". (Though that is merely a default - moment.js probably updates it or has some other mapping at runtime for locale-aware date formats - I would like to access that mapping.)

编辑:

有多个downvotes(谁没有解释为什么他们正在贬低它)。

There are multiple downvotes (who aren't explaining why they're downvoting it).

我认为这是因为他们不理解这个问题,所以这里有一些例子:

I think this is because they arent' understanding the question, so here are some examples:

我想要一个函数:
getFormat(l) - >mm / dd / yyyy,或等效于美国语言环境。对于AU语言环境,
getFormat(l) - >dd / mm / yyyy或等价物。

I want a function such that: getFormat("l") -> "mm/dd/yyyy", or equivalent for the US locales. getFormat("l") -> "dd/mm/yyyy", or equivalent, for the AU locales.

我做想要格式化给定日期,或者解析给定日期 - 我只想确定它是用户友好的格式,给定一个任意的moment.js格式,特别是'l'。

I do not want to format a given date, or to parse a given date - I merely want to determine it's user-friendly format given an arbitruary moment.js format, specifically, for 'l'.

推荐答案

我不认为它暴露得很好,但如果浏览器的语言配置正确,你可以这样做:

I don't think it's exposed nicely, but if the browser has its language configured correctly you can do something like this:

var lang = navigator.languages ? navigator.languages : navigator.language;

moment().locale(lang).localeData()._longDateFormat['L']

语言的行为略有不同,具体取决于您使用的浏览器,因此不知道这有多可靠。

Languages behave slightly differently depending on which browser you're using, so don't know how reliable this is.

这篇关于使用moment.js,如何显示用户的当前日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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