La función JS Date no funciona en Safari

I am making an rails app and getting a date back from the server from an ajax call. The received date from the server is 2012-12-23 18:44:00 UTC. Cuando hago esto newYear = new Date(data) (where data is the received date) I get Sun Dec 23 2012 19:44:00 GMT+0100 (CET) en Chrome y Invalid Date en safari.

(I actually want to get the date from the server and convert it to milliseconds so I can compare it to todays date.)

So it works fine in Chrome, but not in Safari.

¿Como puedo solucionar este problema?

archivo .js:

$ ->
    $(document).ready ->
        $.post "/get_time", (data) ->
            newYear = new Date(data)
            today = new Date().getTime()
            newYearMS = newYear.getTime()
            $("#ggg").html newYearMS
            if newYearMS < today
                $("body").css "background-image", "url('/assets/HypePlay.png')"
            else
                $("#play_button").hide()
                $("#counter").countdown
                    until: newYear
                    format: "HMS"
                    expiryUrl: "/"
                    $('#hhh').html data

return_time function from controller:

def return_time
    date = Item.where(:end => Time.zone.now .. '2040-12-28 08:08:00').order("launch ASC").limit(1).first.launch
    render text: date
  end

preguntado el 23 de diciembre de 12 a las 09:12

What's keeping the server from returning as MS? -

3 Respuestas

You should return the time in miliseconds from the server and then use it in new Date() instead of returning a date string.

La date objeto does accept a lot of different formats but I could imagine the UTC part breaks it in Safari.

Respondido el 23 de diciembre de 12 a las 10:12

+1 Best thing you can do is package UTC as milliseconds whenever passing dates around. I am actually surprised however at just how much the date object can actually handle in terms of pulling dates out of things like 12-21-2012 - Erik Reppen

Thanks, returning MS from the server would be the best. Do you know how I could convert date to MS in my controller? I added the controller function in my question. - allegutta

Sorry not a ruby expert. In php there is the strtotime function for that: php.net/manual/en/function.strtotime.php - Escuchar

It works! :) So I sent the date back to the site in MS from the controller like this: ms = (date.to_f.*1000).to_i. - allegutta

I recommend generating milliseconds or ISO-8601; they can be passed to the date constructor on all major browsers:

new Date("2012-12-20T18:15Z")
Thu Dec 20 2012 13:15:00 GMT-0500 (EST)

If you really can't change the format coming from the server, then you'll need to use something like moment.js:

moment.utc("2012-12-23 18:44:00 UTC", "YYYY-MM-DD HH:mm:ss")

Respondido el 23 de diciembre de 12 a las 10:12

I agree with @Horen, and his advice on passing back the time in milliseconds from the server.

If you're tied with what you have, and you need to work with formats, you might want to take a look at Moment.js. It will also help you perform date/time validation.

Respondido el 23 de diciembre de 12 a las 10:12

Or you could learn how the date constructor works rather than slap a 40.3KB source library into your code and learn how that works. - Erik Reppen

@ErikReppen, 5KB minified. - wulfgarpro

that's why I said source. It's still a bunch of code for one small problem. - Erik Reppen

@ErikReppen, Yeah, but I thought the OP might find it useful considering it provides additional functionality such as parsing/validation. - wulfgarpro

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.