1

Can any one help me to covert the datetime to this

04/01/2017 12:30:05 PM 

format using jquery.

Geeky Ninja
  • 6,002
  • 8
  • 41
  • 54
user123
  • 79
  • 1
  • 11

2 Answers2

0

Using getDate , getMonth , getFullYear and for time use getHours , getMinutes and getSeconds you can do this easily and get format what ever you want.

var formattedDate =new Date("04/01/2017 12:30:05 PM");
var d = formattedDate.getDate();
var m =  formattedDate.getMonth();
m += 1;  // months are 0-11
var y = formattedDate.getFullYear();
var t = formattedDate.getHours() + ":" + formattedDate.getMinutes() + ":" + formattedDate.getSeconds();
console.log(d + "_" + m + "_" + y);
console.log(m + "_" + d + "_" + y);
console.log ('Time: ' + t );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
4b0
  • 21,981
  • 30
  • 95
  • 142
0

If you want to use only jQuery, Try this:

/* Convert your date string to date object */

var strDate = "2017-04-01 12:30:05"
var regex = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
var arrDate = regex.exec(strDate); 
var objDate = new Date(
    (+arrDate[1]),
    (+arrDate[2])-1, // Month starts at 0!
    (+arrDate[3]),
    (+arrDate[4]),
    (+arrDate[5]),
    (+arrDate[6])
);

/* Convert the date object to string with format of your choice */

var newDate = objDate.getMonth() + 1 + '/' + objDate.getDate() + '/' + objDate.getFullYear();

/* Get the time in your format */

var newTime = objDate.toLocaleString('en-US', { hour: 'numeric',minute:'numeric', second: 'numeric', hour12: true });

/* Concatenate new date and new time */

alert(newDate + " " + newTime);

Here is the reference to convert your date string to date object.

Community
  • 1
  • 1
Senjuti Mahapatra
  • 2,570
  • 4
  • 27
  • 38