1

Can any one help me to covert the datetime to this

04/01/2017 12:30:05 PM 

format using jquery.

Geeky Ninja
  • 5,922
  • 8
  • 37
  • 52
user123
  • 79
  • 1
  • 11

3 Answers3

0

I think this can help you

Theres a getDateFromFormat() function that you can tweak a little to solve your problem

OR

var sDate = new Date(Date.parse("XXX","ZZZ"));

Where XXX is your Date string. And ZZZ is your format for XXX.

i.e.

If I want to convert string "2017-04-01 12:30:05" which is in "MM/dd/yyyy hh:mm:ssa" format then

var sDate = new Date(Date.parse("2017-04-01 12:30:05","MM/dd/yyyy hh:mm:ssa"));

Why bother with jQuery? Take a look at datejs.

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
  • 20,627
  • 30
  • 92
  • 137
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,522
  • 4
  • 25
  • 38