0

I want to show the time in a readable format. So I am using the below js code. But the output is different in Chrome and IE. How do I change the code to give the same output across all the browsers ?

The output in IE : 12:46 am

In Chrome : 6:16 am

Time zone is : UTC +05:30

    var unReadableDate = "2016-01-25T00:46:00";
    var newDate = new Date(unReadableDate);
    //var timeZoneOffset = (new Date()).getTimezoneOffset();
    //newDate.setMinutes(newDate.getMinutes() - timeZoneOffset);
    alert(formatAMPM(newDate));

    //below function formats time in am and pm
    function formatAMPM(date) {
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var ampm = hours >= 12 ? 'pm' : 'am';
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0' + minutes : minutes;
        var strTime = hours + ':' + minutes + ' ' + ampm;
        return strTime;
    }
Philipp
  • 67,764
  • 9
  • 118
  • 153
Karthik
  • 1,447
  • 1
  • 18
  • 26
  • For me, Chrome outputs `1:46 am`. The problem might be related to the users timezone. Firefox also outputs `12:46 am`, by the way. – Philipp Feb 14 '16 at 11:14
  • Don't parse strings with the Date constructor, it is unreliable (as you've discovered). An ISO 8601 datetime strings without a timezone should be treated as local (i.e. consider the system timezone offset) however some browsers don't and some will not parse it at all. Manually parse the string, it only requires a 2 line function or library. – RobG Feb 14 '16 at 20:57

3 Answers3

1

Converting a UTC format string to a date using Javascript Date constructor is not reliable. If you want to tackle timezone issues with date you should use moment.js. To understand more you can use below link.

Javascript Date issue

OR simple way to resolve the issue is pass individual arguments in the date instead of complete string. To understand more you can use below link

DateTime UTC

Community
  • 1
  • 1
Nitin Garg
  • 888
  • 6
  • 7
1

Your problem is that your date string being treated as a local time vs it being treated as UTC.

Just make it unambiguous by specifying the time zone. Change

var unReadableDate = "2016-01-25T00:46:00";

to

var unReadableDate = "2016-01-25T00:46:00Z";
potatopeelings
  • 40,709
  • 7
  • 95
  • 119
1

Can you please try replacing this

var unReadableDate = "2012-06-25T00:46:00.000Z"
    var newDate = new Date(unReadableDate);
    //var timeZoneOffset = (new Date()).getTimezoneOffset();
    //newDate.setMinutes(newDate.getMinutes() - timeZoneOffset);
    alert(formatAMPM(newDate));

    //below function formats time in am and pm
    function formatAMPM(date) {
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var ampm = hours >= 12 ? 'pm' : 'am';
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0' + minutes : minutes;
        var strTime = hours + ':' + minutes + ' ' + ampm;
        return strTime;
    }
Nitin Garg
  • 888
  • 6
  • 7