0

var now = new Date('18/10/2016 10:31:22PM');
var time = now.toLocaleTimeString();

alert(time);

this function give an output invalid date. I want to convert this "18/10/2016 22:31:22" format. give me an appropriate example as a solution.

Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • 2
    if you can use http://momentjs.com/ library, it is very easy, moment(date, 'DD/MM/YYYY hh:mm:ssa').format('DD/MM/YYYY HH:mm:ss'), where date is your curent date and it will output the date format that you need – Marko Oct 20 '16 at 10:54
  • This might help: http://stackoverflow.com/questions/15083548/convert-12-hour-hhmm-am-pm-to-24-hour-hhmm – Rajesh Oct 20 '16 at 10:57

2 Answers2

0

this function should work for your date format

function convertDate(date_string){
var d = date_string.trim();
d = d.split(" ");
v = d[1].split(":")[0];
v = d[1].indexOf("PM")>-1 ? +v+12 : v;
d[1] = d[1].replace(d[1].split(":")[0],v);
d = d.join(" ").replace("PM","").replace("AM","");
return d;
}

console.log(convertDate("18/10/2016 10:31:22PM"));
console.log(convertDate("01/10/2016 09:31:22PM"));
console.log(convertDate("06/10/2016 2:31:22AM"));
console.log(convertDate("07/10/2016 7:31:22AM"));
Mamdouh Saeed
  • 2,302
  • 1
  • 9
  • 11
0

Without using a library, you will need to extract all the date tokens and send them into a new date object in the correct order. From there, you can create your own date formatting function.

var DATE_FORMAT = /(\d{1,2})\/(\d{1,2})\/(\d{4}) (\d{1,2}):(\d{2}):(\d{2})([AP]M)/;
var dateStr = '18/10/2016 10:31:22PM';
var now = parseDateString(dateStr, DATE_FORMAT, function(tokens) {
  return [
    parseInt(tokens[3], 10),                  // year
    parseInt(tokens[2], 10) - 1,              // month
    parseInt(tokens[1], 10),                  // date
    to24(parseInt(tokens[4], 10), tokens[7]), // hours, meridiem
    parseInt(tokens[5], 10),                  // minutes
    parseInt(tokens[6], 10),                  // seconds
    0                                         // milliseconds
  ];
});
document.body.innerHTML = formateDate(now);

function parseDateString(dateStr, dateFormat, func) {
  var tokens = dateStr.match(dateFormat);
  var args = Array.prototype.concat.apply([null], func(tokens));
  return new (Function.prototype.bind.apply(Date, args));
}
function formateDate(date, dateSeparator) {
  return [
    pad2(date.getDate()),
    pad2(date.getMonth() + 1),
    date.getFullYear()
  ].join(dateSeparator || '/') + ' ' + [
    pad2(date.getHours()),
    pad2(date.getMinutes()),
    pad2(date.getSeconds())
  ].join(':');
}
function pad2(str) { return ('00' + str).substr(-2); }
function to24(hours, meridiem) {
  switch (meridiem) {
    case 'PM': if (hours < 12) return hours + 12;
    case 'AM': if (hours === 12) return hours - 12;
    default: return hours;
  }
}

Of course, this can be done in moment with one line.

var dateStr = '18/10/2016 10:31:22PM';
var time = moment(dateStr, 'DD/MM/YYYY hh:mm:ssA').format('DD/MM/YYYY HH:mm:ss');
document.body.innerHTML = time;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132