Reputation: 857
On my return response i am getting start_time
like 10:00:00
in 24 hours format.I want to convert it in to two variables on var a ="10:00"
and on var b = "PM"
i tried it several times but not succeed. How can i do this.
Code on my php function :
$time = $actData->start_time; // 14:00:00
$startTime = date('h:i', strtotime($time)); // 02:00
$amPm = date('a', strtotime($time)); // PM
Same i want to do with scripts . How can i do this?
Upvotes: 1
Views: 1733
Reputation: 189
Try this one
function parseTime(timeString)
{
if (timeString == '') return null;
var d = new Date();
var time = timeString.match(/(\d+)(:(\d\d))?\s*(p?)/i);
hrs = parseInt(time[1],10) + ( ( parseInt(time[1],10) < 12 && time[4] ) ? 12 : 0);
return amPm = hrs > 11 ? 'pm' : 'am';
}
time = '14:00:00';
var date = parseTime(time);
print(date);
Upvotes: 0
Reputation: 14313
Here's how you could implement the time conversion:
function convertFromMilitaryToStandard(time) {
var hour = time.slice(0,2),
pm = hour > 12,
hour12 = hour - (pm ? 12 : 0);
//Per the request of the OP, we keep them seperate:
var a = (hour12 < 10 ? "0" : "") + hour12 + time.slice(2,5);
var b = (pm ? "PM" : "AM");
return [a,b];
}
console.log(convertFromMilitaryToStandard("10:00:00"))
console.log(convertFromMilitaryToStandard("14:00:00"))
Upvotes: 1