0

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

Pedro Chiiip
  • 198
  • 2
  • 2
  • 15
Carlos Vieira
  • 559
  • 2
  • 10
  • 22

2 Answers2

0

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

Manas
  • 1
  • 1
  • None of the mentioned "supported" formats is mentioned in ECMA-262. Please don't reference w3schools, ECMA-262 and MDN are much preferred. – RobG Apr 06 '17 at 20:27
0

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:

Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209