I have this date 20/4/2016;4:23:00;PM
But i want to put in like this DD/MM/YYYY HH:mm:ss without the AM or PM
For example 20/04/2016 16:23:00
Thanks in advance Carlos Vieira
I have this date 20/4/2016;4:23:00;PM
But i want to put in like this DD/MM/YYYY HH:mm:ss without the AM or PM
For example 20/04/2016 16:23:00
Thanks in advance Carlos Vieira
In javascript the supported format for in your case is "MM/DD/YYYY hh:mm:ss AM" or "MM/DD/YYYY hh:mm:ss PM". If you will give input like "04/20/2016 11:00:06 PM" then you will get an output as date format "Fri Mar 04 2016 23:00:06 GMT+0530 (IST)" which you can save it. You can put "04/20/2016 11:00:06Z PM" mark here and get the result according to the UTC. For reference https://www.w3schools.com/js/js_date_formats.asp
One approach is to reformat the string and convert the time part to 24hr time, e.g:
function reformatDate(s) {
var b = s.split(';');
var t = b[1].split(':');
var h = +t[0];
h = h%12 + (/pm/i.test(b[2])? 12 : 0);
return b[0] + ' ' + h + ':' + t[1] + ':' + t[2];
}
console.log(reformatDate('20/4/2016;4:23:00;PM')); // 20/4/2016 16:23:00
That avoids the vagaries of parsing the entire string to a date then reformatting it. If that's what you want to do, there are plenty of questions here already on parsing and formatting dates: