0

I have written code in javascript

             if(issuehistory.length) 
             {
               for (var k in issuehistory) {
                       $('.library_info_tbl_books tbody').prepend('<tr>' +
                       ...
                       ...
                      '<td class="text-center centeralign"> ' + issuehistory[k]['due_date'] + '</td>' +
                     '</tr>');
                        console.log(issuehistory[k]['due_date']);
                       }
               }

The output of due date in console appears like

             Object
                  sec: 1510959600
                  usec: 0
                  __proto__
                        :
                        Obj
                     ...
               Object
                  sec: 1510959600
                  usec: 0
                  __proto__
                        :
                        Obj

In browser inside table it is rendered as "[object Object]"

Please help me how to transform it into mm/dd/yyyy format?

Nida
  • 1,672
  • 3
  • 35
  • 68
  • see https://stackoverflow.com/questions/11591854/format-date-to-mm-dd-yyyy-in-javascript. It may help you – Makarand Patil Nov 08 '17 at 10:14
  • 1
    `console.log(new Date(issuehistory[k]['due_date']['sec'] * 1000).toDateString())` – TheChetan Nov 08 '17 at 10:16
  • It is displaying them as Sat Nov 18 2017, I want the date to be in mm/dd/yyyy format – Nida Nov 08 '17 at 10:21
  • Possible duplicate of [function to convert timestamp to human date in javascript](https://stackoverflow.com/questions/19485353/function-to-convert-timestamp-to-human-date-in-javascript) – Bourbia Brahim Nov 08 '17 at 10:22
  • Possible duplicate of [Format date to MM/dd/yyyy in JavaScript](https://stackoverflow.com/questions/11591854/format-date-to-mm-dd-yyyy-in-javascript) – coagmano Nov 08 '17 at 10:23

1 Answers1

3

You can not display an object so you will have to get the value of the sec first. Try

if (issuehistory.length) {
  for (var k in issuehistory) {
    $('.library_info_tbl_books tbody').prepend('<tr>' +
      ...
      ...
      '<td class="text-center centeralign"> ' + (new Date(issuehistory[k]['due_date']['sec'] * 1000).toLocaleDateString()) + '</td>' +
      '</tr>');
    console.log(issuehistory[k]['due_date']);
  }
}
Wouter Bouwman
  • 1,003
  • 6
  • 13
  • In case this fails, just save it to a local variable and then call the `toLocaleDateString()` method on it. – TheChetan Nov 08 '17 at 10:30