2

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. When I do this newYear = new Date(data) (where data is the received date) I get Sun Dec 23 2012 19:44:00 GMT+0100 (CET) in Chrome and Invalid Date in 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.

How can I fix this problem?

.js file:

$ ->
    $(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
allegutta
  • 5,626
  • 10
  • 38
  • 56

3 Answers3

3

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

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

Horen
  • 11,184
  • 11
  • 71
  • 113
  • +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 Dec 23 '12 at 10:04
  • 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 Dec 23 '12 at 10:56
  • Sorry not a ruby expert. In php there is the strtotime function for that: http://php.net/manual/en/function.strtotime.php – Horen Dec 23 '12 at 11:09
  • 1
    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 Dec 23 '12 at 12:07
2

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")
0

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.

wulfgarpro
  • 6,666
  • 12
  • 69
  • 110