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?
var now = new Date(); var utc = new Date(now. getTime() + now. getTimezoneOffset() * 60000);
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.
getTime() returns the number of milliseconds since January 1, 1970 00:00:00.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With