The server is sending a string in this format: 18:00:00
. This is a time-of-day value independent of any date. How to convert it to 6:00PM
in Javasc
function timeConversion(s) {
let hour = parseInt(s.substring(0,2));
hour = s.indexOf('AM') > - 1 && hour === 12 ? '00' : hour;
hour = s.indexOf('PM') > - 1 && hour !== 12 ? hour + 12 : hour;
hour = hour < 10 && hour > 0 ? '0'+hour : hour;
return hour + s.substring(2,8);
}
Make sure that your time is in this format HH:MM:SS(PM/AM)
function timeConversion(s) {
s = s.split(':');
var time = s[2];
if(time.charAt(2) === 'A' && parseInt(s[0]) == 12) s[0] = '00';
if(time.charAt(2) === 'P' && parseInt(s[0]) <12) s[0] = parseInt(s[0])+12;
if(s[0] >=24) s[0]-=24;
var x = time.split('').slice(0,2);
s[2] = x.join('');
console.log(s.join(':'));
}
A simple code for this will be
time = time.split(':');// here the time is like "16:14"
let meridiemTime = time[0] >= 12 && (time[0]-12 || 12) + ':' + time[1] + ' PM' || (Number(time[0]) || 12) + ':' + time[1] + ' AM';
You can adjust according to your time format
function Time_Function() {
var date = new Date()
var time =""
var x= "AM"
if(date.getHours() >12){
x= "PM"
}
time= date.getHours()%12 + x +":"+ date.getMinutes() +":"+ date.getSeconds()
}