How can I format a json date in dd/mm/yy format in javascript?

44,610

Solution 1

I don't think that the other posted answers are quite right, you have already accepted one as working for you so I won't edit it.

Here is an updated version of your accepted answer.

var dateString = "\/Date(1334514600000)\/".substr(6);
var currentTime = new Date(parseInt(dateString ));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = day + "/" + month + "/" + year;
alert(date);

It uses a technique from this answer to extract the epoch from the JSON date.

Solution 2

I found very helpful the row1 answer, however i got stuck on the format for input type="date" as only returns one string for decimals under 10, I was able to modify to work on input type="date", I basically adapted the code from row1 to the code from the link http://venkatbaggu.com/convert-json-date-to-date-format-in-jquery/

I was able through jquery .val add the date to the input

var dateString = "\/Date(1334514600000)\/".substr(6);
var currentTime = new Date(parseInt(dateString));
var month = ("0" + (currentTime.getMonth() + 1)).slice(-2);
var day = ("0" + currentTime.getDate()).slice(-2);
var year = currentTime.getFullYear();
var date = year + '-' + month + '-' + day;
alert(date);
Share:
44,610
andy
Author by

andy

Meet Me and decide ur self.. :-P

Updated on July 28, 2021

Comments

  • andy
    andy almost 3 years

    I have a json date like \/Date(1334514600000)\/ in my response and when I convert it in javascript then I got this date Tue Apr 17 2012 11:37:10 GMT+0530 (India Standard Time), but I need the date format like 17/04/2012 and I fail every time. Can anyone tell me how can I resolve it?