11

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?

scessor
  • 15,995
  • 4
  • 43
  • 54
andy
  • 595
  • 3
  • 8
  • 23

8 Answers8

18

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.

Community
  • 1
  • 1
row1
  • 5,568
  • 3
  • 46
  • 72
  • You can also use `var date = new Date(+dateString.match(/\d+/)[0]);` to convert the time to a date. Be careful though as the date object will be in the timezone of the system where the code is executed. – RobG Apr 17 '12 at 08:30
  • Thanks for this question and response! – Shane LeBlanc May 08 '12 at 17:54
  • This gives me 2/1/2019. how do I get 02/01/2019? – kas_miyulu Jul 24 '18 at 16:01
  • Replying to KMR question ============================================================================================================== 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); – Surya R Praveen Feb 28 '23 at 14:12
1

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);
silvajnr
  • 31
  • 1
  • 4
1

Here is an updated version of your accepted answer. DD/MM/YYYY Format Get Try This..

var dateString = "/Date(1623781800000+0530)/"+.substr(6);
var currentTime = new Date(parseInt(dateString));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
 if (month.toString().length == 1) 
   month = "0" + month.toString();
 if (day.toString().length == 1){ 
   day = "0" + currentTime.getDate();}                                       
var datenew = day + "/" + month + "/" + year;
0
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var date = day + "/" + month + "/" + year
alert(date);
Elvis D'Souza
  • 2,273
  • 1
  • 23
  • 31
  • I'm trying this but it is not working var currentTime = new Date($("input").val()); var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() var date = day + "/" + month + "/" + year alert(date); – andy Apr 17 '12 at 06:50
0

It's answer to your question...

Build the date object with your 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);​

it works

http://jsfiddle.net/ChgUa/

Mr_DeLeTeD
  • 825
  • 1
  • 6
  • 18
  • You are missing the step where you convert `\/Date(1334514600000)\/` to `new Date(1334514600000)` – row1 Apr 17 '12 at 07:03
0
//parse JSON formatted date to javascript date object
var bdate = new Date(parseInt(emp.Birthdate.substr(6)));

//format display date (e.g. 04/10/2012)
var displayDate = $.datepicker.formatDate("mm/dd/yy", bdate);
Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
ujval
  • 51
  • 1
  • 3
0

Easiest way of formatting date is by using pipes if you are using Angular. Click here

//in .ts file
ngOnInit() {
 this.currentDate = new Date()
}
//in html file
<p>Current date is:</p>{{currentDate | date: 'dd/MM/yyyy'}}

//Output: 22/04/2020
David Buck
  • 3,752
  • 35
  • 31
  • 35
-1
 var Date = new Date(Tue Jun 15 2021 23:52:47 GMT+0800 (Malaysia Time)).toDateString(); console.log(Date);

Result == Tue Jun 15 2021


  • This doesn't answer the OP's question. Also, this is syntactically invalid, you need to surround the `Tue Jun 15...` with quotes. – Nate Levin Jun 15 '21 at 17:40