Alex Kumbhani Alex Kumbhani - 1 month ago 7
Javascript Question

how to get time with some specific format?

how should i getTime with some specific format like i want to get time hh:mm am/pm and then add some minute .
example i have date

2016-09-16T11:40:50.000Z
from it first get time
11:40 am
and then add 20 minutes and get result like
12:00 am

so, how can i do this task.
i try date.getTime() but it not give me proper time which i want.

Answer
function gettime(date) {
 var hours = date.getHours();
 var minutes = date.getMinutes();
 var ampm = hours >= 12 ? 'pm' : 'am';
 hours = hours % 12;
 hours = hours ? hours : 12; // the hour '0' should be '12'
 minutes = minutes < 10 ? '0'+minutes : minutes;
 return = hours + ':' + minutes + ' ' + ampm;

}

Comments