简体   繁体   English

使用moment.js获取AM或PM的当前时间

[英]fetch current time in AM or PM using moment.js

get the current time in 12 hr format using moment.js 使用moment.js以12小时格式获取当前时间

currently i am trying to fetch the hrs and mins as follows , but current code is returning only hrs and min i want AM and PM too based on current tiime 目前,我正在尝试获取小时和分钟,如下所示,但当前代码仅返回小时和分钟,我也希望基于当前时间的上午和下午

getHrMin:function(){

            var currentDate = moment();
            var data = currentDate.format("hh:mm").split(":");

            if(data.length >0 )
                return {"hrs":data[0], "min":data[1]};
            else
                return null;


        }

As per the documentation , mentioned on the moment website, the format to receive AM or PM notation is a or A which returns the am or pm format in small letters(a) or capital letters(A). 根据当前网站上提到的文档 ,接收AM或PM表示法的格式为aA ,以小写字母(a)或大写字母(A)的形式返回am或pm格式。

So in your case, you could do 因此,就您而言,您可以

    currentDate.format("hh A:mm").split(":")

in order to get the am or pm format. 为了获得上午或下午的格式。

The value passed to format function above depends on how you want to format it. 上面传递给格式函数的值取决于您要格式化的方式。

In the above case the format will return something like 11 AM:30 and when you split it by colon(:), you will receive 2 elements in array, first will be 11 AM and second will be 30 . 在上述情况下,格式将返回类似于11 AM:30的内容,并且当您通过冒号(:)分割它时,您将在数组中收到2个元素,第一个为11 AM ,第二个为30 In case you don't want the AM or PM value with the hour value , you could do 如果您不希望将AM或PM值与小时值一起使用,可以这样做

  format("hh:A:mm")

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM