I need to reproduce the following Ruby code in Javascript but I can't find an equivalent for to_i.
beginning_of_last_full_hour = (Time.now.to_s.split(/:\d{2}/)[0] + ":00:00").to_time.to_i - 3600
Any help would be appreciated. I am trying to get the beginning of the last full hour. Thanks
2 Answers 2
Javascript equivalent of Ruby to_i
parseInt().
Follow this for more details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
Converting following code from Ruby to JavaScript:
(Time.now.to_s.split(/:\d{2}/)[0] + ":00:00").to_time.to_i
# => 1426788000
# Time.now # => 2015年03月19日 18:45:22 +0530
JS equivalent:
d = new Date();
new Date( d.toString().split(/:\d{2}/)[0] + ":00:00").getTime();
# 1426788000
3 Comments
Seems like you want to take unix timestamp. In this case you should convert your Date object to it use:
date_object / 1000
or
date_object.getTime() / 1000
You should divide by 1000 because to_i for Ruby Time object return unix timestamp in seconds but getTime() return it in milliseconds.
to_iisn't really the important part. You need to convert a time object to a unix timestamp.