Extract time from moment js object

2020-02-03 09:44发布

How do i extract the time using moment.js?

"2015-01-16T12:00:00"

It should return "12:00:00 pm". The string return will be passed to the timepicker control below.

http://jdewit.github.com/bootstrap-timepicker 

Any idea?

4条回答
做自己的国王
2楼-- · 2020-02-03 10:13

This is the good way using formats:

const now = moment()
now.format("hh:mm:ss K") // 1:00:00 PM
now.format("HH:mm:ss") // 13:00:00

Red more about moment sring format

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-03 10:17

Use format method with a specific pattern to extract the time. Working example

var myDate = "2017-08-30T14:24:03";
console.log(moment(myDate).format("HH:mm")); // 24 hour format
console.log(moment(myDate).format("hh:mm a")); // use 'A' for uppercase AM/PM
console.log(moment(myDate).format("hh:mm:ss A")); // with milliseconds
<script src="https://momentjs.com/downloads/moment.js"></script>

查看更多
等我变得足够好
4楼-- · 2020-02-03 10:28

If you read the docs (http://momentjs.com/docs/#/displaying/) you can find this format:

moment("2015-01-16T12:00:00").format("hh:mm:ss a")

See JS Fiddle http://jsfiddle.net/Bjolja/6mn32xhu/

查看更多
不美不萌又怎样
5楼-- · 2020-02-03 10:31

You can do something like this

var now = moment();
var time = now.hour() + ':' + now.minutes() + ':' + now.seconds();
time = time + ((now.hour()) >= 12 ? ' PM' : ' AM');
查看更多
登录 后发表回答