【问题标题】:Moment.js : does en locale should display time in 12 hours format?Moment.js:语言环境是否应该以 12 小时格式显示时间?
【发布时间】:2015-05-27 08:36:31
【问题描述】:

我正在使用 Moment.js 2.10.3。 根据 moment-with-locales.js :

var defaultLongDateFormat = {
    LTS  : 'h:mm:ss A',
    LT   : 'h:mm A',
    L    : 'MM/DD/YYYY',
    LL   : 'MMMM D, YYYY',
    LLL  : 'MMMM D, YYYY LT',
    LLLL : 'dddd, MMMM D, YYYY LT'
};

这是否意味着 en locale 应该以 12 小时格式和 LTS 格式显示时间?

a jsfiddle demonstrating the issue

对不起,我现在的英文不太好……

var locale = "en";
var timeOffset = 540;

var currentTimeOffset = moment().utcOffset(timeOffset).locale(locale).format("LTS");
document.getElementById("timeOffset").innerHTML = currentTimeOffset;

var elLocale = moment.utc(currentTimeOffset, "LTS", 'el');

document.getElementById("elLocaleHours").innerHTML = elLocale.hours();
document.getElementById("elLocaleMinutes").innerHTML = elLocale.minutes();
document.getElementById("elLocaleSeconds").innerHTML = elLocale.seconds(); 


var enLocale = moment.utc(currentTimeOffset, "LTS", locale); 

document.getElementById("enLocaleHours").innerHTML = enLocale.hours();
document.getElementById("enLocaleMinutes").innerHTML = enLocale.minutes();
document.getElementById("enLocaleSeconds").innerHTML = enLocale.seconds(); 

var beLocale = moment.utc(currentTimeOffset, "LTS", 'be'); 

document.getElementById("beLocaleHours").innerHTML = beLocale.hours();
document.getElementById("beLocaleMinutes").innerHTML = beLocale.minutes();
document.getElementById("beLocaleSeconds").innerHTML = beLocale.seconds(); 
<script src="http://momentjs.com/downloads/moment-with-locales.min.js"></script>
<div id="timeOffset"></div>
<div id="elLocale"> elLocale (should be 12 h format) :
    <span id="elLocaleHours"></span>&nbsp; : &nbsp;
    <span id="elLocaleMinutes"></span>&nbsp; : &nbsp;
    <span id="elLocaleSeconds"></span>
</div>
<div id="enLocale"> enLocale (should be 12 h format) :
    <span id="enLocaleHours"></span>&nbsp; : &nbsp;
    <span id="enLocaleMinutes"></span>&nbsp; : &nbsp;
    <span id="enLocaleSeconds"></span>
</div>

<div id="beLocale"> beLocale (should be 24 h format) :
    <span id="beLocaleHours"></span>&nbsp; : &nbsp;
    <span id="beLocaleMinutes"></span>&nbsp; : &nbsp;
    <span id="beLocaleSeconds"></span>
</div>

【问题讨论】:

    标签: javascript time momentjs


    【解决方案1】:

    根据文档,我希望 enLocale.hours() 始终返回 0 - 23 范围内的小时数:

    http://momentjs.com/docs/#/durations/hours/

    与其他持续时间的吸气剂一样,moment.duration().hours() 获取小时数 (0 - 23)。

    http://momentjs.com/docs/#/get-set/hour/

    接受 0 到 23 之间的数字。如果超出范围,则会冒泡 直到今天。

    您可能希望使用基于特定语言环境的format() 来显示它:

    由于首选格式因区域设置而异,因此有一些 可用于根据区域设置格式化时刻的标记。

    【讨论】:

    • 但我需要一个 Date 对象来使用标准方法获取小时/分钟/秒。由于语言环境不同,解析字符串不是一个好主意。在我的示例中,我们可以看到不同区域设置的不同时间格式。
    • @Sergey:抱歉,那你的问题是什么? enLocale 似乎正是标准方法的日期对象。如果您想将日期输出为本地化字符串,您将使用格式化程序。你能再试着解释一下你的问题吗?
    • 在我的示例中,一些语言环境使用 12h 格式,而另一些使用 24 格式:code 2:56:01 PM elLocale(应该是 12h 格式):2:56:1 enLocale(应该是12 小时格式) : 14 : 56 : 1 beLocale (应该是 24 小时格式) : 2 : 56 : 1 code
    • 是错误还是功能?
    猜你喜欢
    • 2011-09-04
    • 2022-08-08
    • 2011-10-15
    • 2017-10-24
    • 2020-07-10
    • 2021-09-26
    • 2010-09-10
    • 2016-03-29
    • 2014-04-15
    相关资源
    最近更新 更多