Javascript 从时刻 js 对象中提取时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27978819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract time from moment js object
提问by ove
How do i extract the time using moment.js?
我如何使用 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.
它应该返回“12:00:00 pm”。字符串返回将传递给下面的时间选择器控件。
http://jdewit.github.com/bootstrap-timepicker
Any idea?
任何的想法?
回答by Pochen
If you read the docs (http://momentjs.com/docs/#/displaying/) you can find this format:
如果您阅读文档(http://momentjs.com/docs/#/displaying/),您可以找到以下格式:
moment("2015-01-16T12:00:00").format("hh:mm:ss a")
See JS Fiddle http://jsfiddle.net/Bjolja/6mn32xhu/
见 JS Fiddle http://jsfiddle.net/Bjolja/6mn32xhu/
回答by Ancient
You can do something like this
你可以做这样的事情
var now = moment();
var time = now.hour() + ':' + now.minutes() + ':' + now.seconds();
time = time + ((now.hour()) >= 12 ? ' PM' : ' AM');
回答by uruapanmexicansong
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
红色更多关于moment sring 格式
回答by Deepu Reghunath
Use formatmethod with a specific pattern to extract the time.
Working example
使用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>

