Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format JSON Datetime on client side by Javascript or Jquery [duplicate]

Possible Duplicate:
Convert a date to string in Javascript

I have date in json format at client side :

/Date(1352745000000)/

The code which i have tried to parse Json date:

eval(dateTime.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));

and

new Date(parseInt(dateTime.substr(6)));

Out put I am getting:

Tue Nov 27 2012 00:00:00 GMT+0530 (India Standard Time)

Desire Output

 2012-11-27 11:16

I am not able to figure out how we will get this.

like image 534
Nishant Kumar Avatar asked May 08 '26 10:05

Nishant Kumar


2 Answers

var date = new Date(parseInt(dateTime.substr(6)));
var formatted = date.getFullYear() + "-" + 
      ("0" + (date.getMonth() + 1)).slice(-2) + "-" + 
      ("0" + date.getDate()).slice(-2) + " " + date.getHours() + ":" + 
      date.getMinutes(); 

Best not to try save space with this one :)

var str, year, month, day, hour, minute, d, finalDate;

str = "/Date(1352745000000)/".replace(/\D/g, "");
d = new Date( parseInt( str ) );

year = d.getFullYear();
month = pad( d.getMonth() + 1 );
day = pad( d.getDate() );
hour = pad( d.getHours() );
minutes = pad( d.getMinutes() );

finalDate =  year + "-" + month + "-" + day + " " + hour + ":" + minutes;

function pad( num ) {
    num = "0" + num;
    return num.slice( -2 );
}
like image 23
Bruno Avatar answered May 10 '26 01:05

Bruno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!