0

My code is using datatable in which i use render date function it works in chrome but it shows NaN in IE11 & Safari. Pls help me how to resolve this issue Here is my code snippet
Datatable

 {
    "data": 'starttime',
    "render": function (data) {
        var date = new Date(data);
        var month = date.getMonth() + 1;
        return (month.length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear() + " - " + date.getHours() + ":" + date.getMinutes();
    }
}

Js code

var date = new Date(driver_data[i].starttime);
var month = date.getMonth() + 1;
ps = (month.length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear() + " - " + date.getHours() + ":" + date.getMinutes();
Tamara
  • 83
  • 1
  • 1
  • 9
  • What is your date format ?? See https://stackoverflow.com/questions/43316829/new-dateyyyy-mm-not-work-on-ie-11 – 4b0 Mar 26 '19 at 06:39
  • @Shree this the date which i get from server "2019-03-18T12:30:00.000+0530" – Tamara Mar 26 '19 at 06:43
  • Just give date without time like `2019-03-18` and it's fine. – 4b0 Mar 26 '19 at 06:50
  • @Shree i get this date from api response & i want to display date & time both as per requirement. I tried with using .replace(/\//g, "-"), but not working – Tamara Mar 26 '19 at 06:52
  • So you need to split date and time and hold that on some variable and pass only date part for new date and concatenate again for final result. – 4b0 Mar 26 '19 at 06:56
  • Could you pls help me with an example using my code – Tamara Mar 26 '19 at 07:00
  • See this fiddle . https://jsfiddle.net/do2yb63n/ – 4b0 Mar 26 '19 at 07:17
  • @Shree not working showing an error "cannot read property split of null" – Tamara Mar 26 '19 at 07:37
  • Naaaa ie support split function see . Just ensure dt is not empty like fiddle example. https://stackoverflow.com/questions/6422355/javascript-split-function-not-working-in-ie – 4b0 Mar 26 '19 at 08:12
  • I tested on IE and it's work. – 4b0 Mar 26 '19 at 08:22

2 Answers2

0

Try this

{
    "title": "D.O.B",
    "data": "dob",
    "render": function (data) {
         var d = new Date(data),
         month = d.getMonth() + 1,
         day = d.getDate(),
         year = d.getFullYear(),
         hour = d.getHours(),
         minutes = d.getMinutes(),
         seconds = d.getSeconds();
         return day + ' – ' + month + ' – ' + year + ' ' + hour + ':' + minutes + ':' + seconds;        
     }
}
Karthik
  • 31
  • 1
  • 3
  • The code which I shared is working fine in IE & safari, I think you need to check the Date format properly – Karthik Mar 26 '19 at 08:21
0

Older versions of Safari, IE do not support parsing of ISO format string dates and therefore you are getting the NaN errors. You could implement your own parsers or just use moment.js library which makes it a whole lot easier.

{
    "data": 'starttime',
    "render": function (data) {
        return moment(data).format("MM/DD/YYYY-hh:mm");
    }
}

Here are more ways to format

deviprsd
  • 378
  • 3
  • 12
  • how i use moment in code "var date = new Date(driver_data[i].starttime); var month = date.getMonth() + 1; ps = (month.length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear() + " - " + date.getHours() + ":" + date.getMinutes();" – Tamara Mar 26 '19 at 08:30