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

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

+6
source share
5 answers

I do not think that the other published answers are completely correct, you have already accepted it as working for you, so I will not 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); 

He uses a technique from this to extract an era from a JSON date.

+12
source
 var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() var date = day + "/" + month + "/" + year alert(date); 
0
source

The answer to your question...

Create a date object with a timestamp

 var currentTime = new Date(1334514600000) var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() var date = day + "/" + month + "/" + year alert(date);​ 

works

http://jsfiddle.net/ChgUa/

0
source

I really liked row1's answer, however, I was stuck in the format for input type = "date", since it returns only one line for decimal places up to 10, I managed to modify it to work with input type = "date", I basically adapted the code from line1 to the code from the link http://venkatbaggu.com/convert-json-date-to-date-format-in-jquery/

I managed to add input date through jquery.val

 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); 
0
source
 //parse JSON formatted date to javascript date object var bdate = new Date(parseInt(emp.Birthdate.substr(6))); //format display date (eg 04/10/2012) var displayDate = $.datepicker.formatDate("mm/dd/yy", bdate); 
0
source

Source: https://habr.com/ru/post/913389/


All Articles