从 moment js 对象中提取时间
Extract time from moment js object
如何使用 moment.js 提取时间?
"2015-01-16T12:00:00"
它应该 return “12:00:00 pm”。
字符串 return 将传递给下面的时间选择器控件。
http://jdewit.github.com/bootstrap-timepicker
有什么想法吗?
你可以这样做
var now = moment();
var time = now.hour() + ':' + now.minutes() + ':' + now.seconds();
time = time + ((now.hour()) >= 12 ? ' PM' : ' AM');
如果您阅读文档 (http://momentjs.com/docs/#/displaying/),您会发现这种格式:
moment("2015-01-16T12:00:00").format("hh:mm:ss a")
见 JS Fiddle http://jsfiddle.net/Bjolja/6mn32xhu/
这是使用格式的好方法:
const now = moment()
now.format("hh:mm:ss K") // 1:00:00 PM
now.format("HH:mm:ss") // 13:00:00
红色更多关于moment sring format
使用具有特定模式的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>
如何使用 moment.js 提取时间?
"2015-01-16T12:00:00"
它应该 return “12:00:00 pm”。 字符串 return 将传递给下面的时间选择器控件。
http://jdewit.github.com/bootstrap-timepicker
有什么想法吗?
你可以这样做
var now = moment();
var time = now.hour() + ':' + now.minutes() + ':' + now.seconds();
time = time + ((now.hour()) >= 12 ? ' PM' : ' AM');
如果您阅读文档 (http://momentjs.com/docs/#/displaying/),您会发现这种格式:
moment("2015-01-16T12:00:00").format("hh:mm:ss a")
见 JS Fiddle http://jsfiddle.net/Bjolja/6mn32xhu/
这是使用格式的好方法:
const now = moment()
now.format("hh:mm:ss K") // 1:00:00 PM
now.format("HH:mm:ss") // 13:00:00
红色更多关于moment sring format
使用具有特定模式的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>