1

I'm using Highcharts library to show values on a website. The xAxis of my table is date and works fine on Firefox and Chrome, but isn't working on Safari browser.

I suspect the interpretation of the Date object in JavaScript with Safari is different form other browsers.

¿Some hint?

This is my code:

<script>
myChart = new Highcharts.chart('containerTemperatura', {
    title: {
        text: 'Evolución de la Temperatura'
    },
    xAxis: {
        type: 'datetime',
        dateTimeLabelFormats: {
            minute: '%H:%M',
            hour: '%H:%M'
        }
    },
    yAxis: {
        title: {
            text: 'ºC'
        }
    },
    series: [{
        name: 'TEMPERATURA',
        color: '#808080', 
        data:(function(){
            var data = [];
            <?php 
            for($i = 0; $i < count($ultimasLecturas); $i++){
                ?>
                // EXAMPLE: $ultimasLecturas[$i]->fechaHora = "2017-08-12 12:34:04" 
                var $fecha = new Date("<?php echo $ultimasLecturas[$i]->fechaHora;?>");

                // a compensation for different timezones
                $fechaProcesada = $fecha.getTime() + <?php echo $UTCmilseg; ?>;
                data.push([$fechaProcesada,<?php echo $ultimasLecturas[$i]->temperatura;?>]);
                <?php
            } ?>
            return data;
        })()
    }]
});
</script>
Maktest
  • 21
  • 6
didaquis
  • 93
  • 2
  • 10
  • 1
    In Safari, `new Date("2017-08-12 12:34:04")` returns an invalid Date (which is compliant with ECMA-262). See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) Note that the Date constructor and *Date.parse* use the same built-in parser. – RobG Aug 27 '17 at 22:26
  • 2
    This is a duplicate of [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Aug 27 '17 at 23:26
  • Possible duplicate of [Why does Date.parse give incorrect results?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – Kacper Madej Aug 28 '17 at 12:14
  • Thank you RobG and Kacper Madej for helping me understand the issue. – didaquis Jan 05 '18 at 20:15

1 Answers1

1

I resolved the question adding this JavaScript function:

function parseDate(inputTimestamp){
    var parts = inputTimestamp.split(/[ \/:-]/g);
    var dateFormated = parts[1] + "/" + parts[2] + "/" + parts[0] + " " + parts[3] + ":" + parts[4] + ":" + parts[5];
    return new Date(dateFormated);
}

And changing this line...

var $fecha = new Date("<?php echo $ultimasLecturas[$i]->fechaHora;?>");

for this other line:

var $fecha = parseDate("<?php echo $ultimasLecturas[$i]->fechaHora;?>");
didaquis
  • 93
  • 2
  • 10