JavaScript/jQuery - Convert from 24 Hr Datetime String to 12Hr Date format? -
var timezone ="cdt" var startdatetime = "2016-06-15 22:30:00.0"; this.outdate = function() { return getjusttime(startdatetime,timezone); } function getjusttime(startdatetime,timezone) { outdt = new date(startdatetime.replace(/ /g,'t')); return outdt; } **expected output** this.outdate = "10.30 pm cdt";
i have 2 variables above 24 hour datetime string , want convert 12 hour format date string. missing in missing?
p.s : can't use datetime librarires.
just write own. date object helpful.
function am_or_pm (date) { var date_obj = new date(date); var hours = date_obj.gethours(); var morn_or_night; // wouldn't in production, make logic clear - use conditional operator here. // handling midnight if (hours === 0) { hours = 12; morn_or_night = 'am'; // handling noon } else if (hours === 12) { morn_or_night = 'pm' } else if (hours > 12) { hours = hours - 12; morn_or_night = 'pm'; } else { morn_or_night = 'am'; } return hours.tostring()+':'+date_obj.getminutes()+' '+morn_or_night; }