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?
This question is related to
javascript
datetime
time
momentjs
angular-moment
Use format
method with a specific pattern to extract the time.
Working example
var myDate = "2017-08-30T14:24:03";_x000D_
console.log(moment(myDate).format("HH:mm")); // 24 hour format_x000D_
console.log(moment(myDate).format("hh:mm a")); // use 'A' for uppercase AM/PM_x000D_
console.log(moment(myDate).format("hh:mm:ss A")); // with milliseconds
_x000D_
<script src="https://momentjs.com/downloads/moment.js"></script>
_x000D_
You can do something like this
var now = moment();
var time = now.hour() + ':' + now.minutes() + ':' + now.seconds();
time = time + ((now.hour()) >= 12 ? ' PM' : ' AM');
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
Source: Stackoverflow.com