Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Date to UTC for use in javascript

I have a date type field, 2011-07-20 as a format example, that I need to convert to UTC for use in javascript. I know I can use Date.UTC in javascript but then the month is off by one and it doesn't take the dashes as delimiters.

How do you convert the default rails date format to UTC?

like image 532
Xaxum Avatar asked Aug 29 '11 23:08

Xaxum


People also ask

How do you convert date to UTC format?

var now = new Date(); var utc = new Date(now. getTime() + now. getTimezoneOffset() * 60000);

What is UTC time in JavaScript?

UTC() method in JavaScript is used to return the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time. The UTC() method differs from the Date constructor in two ways: Date. UTC() uses universal time instead of local time.

How to get time from a date in JavaScript?

getTime() returns the number of milliseconds since January 1, 1970 00:00:00.

Does new date return UTC?

The Date object internally stores only a number of milliseconds since 1970-01-01T00:00:00Z (without consideration of leap seconds). In other words, Date objects are always representing the UTC time.


1 Answers

In a controller

@time = Time.parse('2011-07-20').utc.to_i*1000

In a view

<script type="text/javascript">
    var date = new Date(<%= @time %>);
    alert(date);
</script>
like image 118
kreek Avatar answered Oct 04 '22 20:10

kreek