从时刻 js 对象中提取时间

Extract time from moment js object

本文关键字:提取 取时间 对象 时刻 js      更新时间:2023-09-26

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

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

它应返回"12:00:00 pm"。字符串返回将传递给下面的时间选取器控件。

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

知道吗?

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

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

参见 JS 小提琴 http://jsfiddle.net/Bjolja/6mn32xhu/

你可以做这样的事情

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

使用具有特定模式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>

这是使用格式的好方法:

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

红色 更多关于力矩戒指格式