从时刻js对象中提取时间

时间:2015-01-16 07:00:38

标签: javascript datetime time momentjs angular-moment

如何使用moment.js提取时间?

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

它应该返回" 12:00:00 pm"。 字符串返回将传递给下面的timepicker控件。

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

有什么想法吗?

4 个答案:

答案 0 :(得分:58)

如果你阅读了文档(http://momentjs.com/docs/#/displaying/),你可以找到以下格式:

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

参见JS Fiddle http://jsfiddle.net/Bjolja/6mn32xhu/

答案 1 :(得分:17)

你可以做这样的事情

var now = moment();
var time = now.hour() + ':' + now.minutes() + ':' + now.seconds();
time = time + ((now.hour()) >= 12 ? ' PM' : ' AM');

答案 2 :(得分:0)

这是使用格式的好方法:

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

进一步了解moment sring format

答案 3 :(得分:0)

使用具有特定模式的format方法提取时间。 工作示例

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>

相关问题