如何使用moments.js和angularjs获取子午线(上午/下午) [英] How to get the meridian (am / pm) with momentsjs and angularjs
问题描述
我对库moments.js有疑问,我在angularjs中有一个应用程序,其中有六个选择元素,分别用于年,月,日,小时,分钟和am/pm格式.我正在使用以下格式来构建m.format日期('YYYY-MM-DD hh:mm:ss a).
I have a question about the library moments.js, I have an application in angularjs where I have six select elements for the year, month, day, hour, minutes, and am / pm format. I am using the following format moment to build m.format date ('YYYY-MM-DD hh: mm: ss a).
代码如下:
var m = moment([scope.val.year, scope.val.month, scope.val.date, scope.val.hour, scope.val.minute]); //build date
model.$setViewValue(m.format('YYYY-MM-DD hh:mm:ss a'))
我得到了数据m.hour ()
,m.minute ()
等,但是有一种获取am/pm格式的方法,我没有找到任何有关它的信息,也许是m.meridian () => "am "or" pm "
.
I get the data m.hour ()
, m.minute ()
, etc but there is a way to get the am / pm format, I have not found anything about it, something perhaps as m.meridian () => "am "or" pm "
.
如果要创建一个数组作为参数,如果是am或pm,则可以从任何日期获取.
I would build the array passing as parameter if am or pm, and then also would get from any date if am or pm date.
推荐答案
几件事:
The word you're looking for is "meridiem", not "meridian". It's a Latin word meaning "mid-day". A.M. is "ante meridiem" (before mid-day) and P.M. is "post meridiem" (after mid-day).
.hours()
函数和传递给moment
构造函数的输入数组都期望24小时制的小时,从0到23.没有内置函数可以接受或输出12.小时时钟值或子午线(am/pm).
The .hours()
function and the input array passed to the moment
constructor both expect hours of a 24-hour clock, from 0 to 23. There is no built-in function to accept or output a 12-hour clock value, or a meridiem (am/pm).
目前的解析和格式化功能确实具有12小时时钟小时(h
或hh
)和子午线(A
或a
)的选项,但您没有解析或在您的用例中进行格式化,因此使用起来会有些笨拙.特别是,它们基于一刻的当前语言环境设置,因此,如果语言环境未固定为特定语言,则测试字符串将是有问题的.
The parsing and formatting functions in moment do have options for 12-hour clock hours (h
or hh
) and meridiem (A
or a
), but you're not parsing or formatting in your use case, so they would be a bit clumsy to use. In particular, they are based on moment's current locale settings, so testing a string would be problematic if the locale wasn't fixed to a specific language.
您可以使用这些简单的功能将12小时以上的子午后转换为24小时,反之亦然.它们不是特定于时刻的,因此您也可以将它们与Date
对象或其他地方一起使用.
You can use these simple functions to convert from 12-hour + meridiem to 24-hour and vice-versa. They are not specific to moment, so you can also use them with the Date
object or elsewhere.
function hours12to24(h, pm) {
return h == 12 ? pm ? 12 : 0 : pm ? h + 12 : h;
}
function hours24to12(h) {
return {
hour : (h + 11) % 12 + 1,
pm : h >= 12
}
}
要测试这些功能,请执行以下操作:
To test these functions:
function test() {
for (var i = 0; i <= 23; i++) {
var x = hours24to12(i);
var h = hours12to24(x.hour, x.pm);
console.log(i + " == " + x.hour + (x.pm ? " pm" : " am") + " == " + h);
}
}
此外,在调用.month()
以获得月份数时要小心,结果为0-11,而不是1-12.构建输入数组时也是如此.您的下拉菜单需要使用0-11,或者需要相应地加减1.
Also, be careful when calling .month()
to get the month number, the results are 0-11, not 1-12. The same is true when building the input array. Your dropdown either needs to use 0-11, or you need to add or subtract 1 accordingly.
这篇关于如何使用moments.js和angularjs获取子午线(上午/下午)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!