Time conversion between ruby on rails and javascript vice versa?
Perhaps the most reliable way is to use seconds since the epoch for ruby, and milliseconds for JavaScript.
In ruby:
t = Time.now
# => 2014-03-12 11:18:29 -0700
t.to_f * 1000 # convert to milliseconds since 1970-01-01 00:00:00 UTC.
# => 1394648309130.185
This value can be directly given to the JavaScript Date constructor:
var d = new Date(1394648309130.185)
d // Wed Mar 12 2014 11:18:29 GMT-0700 (Pacific Daylight Time)
d.getTime() // 1394648309130 (Fractions of a millisecond are dropped)
The output of d.getTime()
divided by 1000 can be given to ruby's Time.at()
:
Time.at( 1394648309130 / 1000.0 )
# => 2014-03-12 11:18:29 -0700
How to parse date with time zone from a custom string?
Normally JS new Date
returns like this.
(new Date).toString(); // => "Fri Oct 30 2015 00:36:43 GMT+0900 (JST)"
I guess your JS datetime string is missing +
after GMT
.
This works perfectly
'Thu Oct 29 2015 15:46:19 GMT+0100 (CET)'.to_datetime # => Thu, 29 Oct 2015 15:46:19 +0100
Calculate time from an array and convert to time zone
You have to calculate the average on the gap from midnight.
A not elegant (but fast) solution could be:
# Keep only time
bed_times.map! { |bt| Time.parse(bt.split(" ")[1]) }
# calculate the gap from 00:00:00
gap_from_midnight = bed_times.map do |bt|
if bt > Time.parse("12:00:00")
gap = (bt.to_f - Time.parse("24:00:00").to_f)
else
gap = (bt.to_f - Time.parse("00:00:00").to_f)
end
gap.to_i
end
# average in sec
avg_in_sec = gap_from_midnight.inject(:+) / bed_times.size
# average in UTC time zone
avg = Time.at(avg_in_sec).utc # => 1970-01-01 05:26:57 UTC (result for bed_times array)
# average in PST time zone (see note)
avg_pst = Time.parse(avg.to_s).in_time_zone("Pacific Time (US & Canada)") # => Wed, 31 Dec 1969 21:26:57 PST -08:00 (result for bed_times array)
# Keep only time
avg_pst.strftime("%H:%M:%S") # => "21:26:57" (result for bed_times array)
With your bed_times
array (with the values as a string)
bed_times = [
"2015-12-10 05:58:24 UTC",
"2015-12-09 03:35:28 UTC",
"2015-12-08 06:32:26 UTC",
"2015-12-07 01:43:28 UTC",
"2015-12-05 07:49:30 UTC",
"2015-12-04 07:02:30 UTC"
]
the average is :
05:26:57
in UTC zone21:26:57
in PST zone
With another array like this
bed_times = [
"2015-12-10 01:00:00 UTC",
"2015-12-09 23:00:00 UTC",
"2015-10-19 18:00:00 UTC",
]
the average is:
22:00:00
in UTC zone14:00:00
in PST zone
note: .in_time_zone
is a helper from ActiveSupport::TimeWithZone
http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html
Ruby/Rails - Convert Unix Timestamp to HH:MM am/pm with Timezone
You have basically two alternatives:
- server-side approach
- client-side approach using JavaScript
I explained both in this answer no more than a few days ago.
How to convert BSON::Timestamp to ruby time and vice versa
You can convert a BSON::Timestamp
to a BSON::ByteBuffer
using the #to_bson
method.
You can then convert the BSON::ByteBuffer
to an integer (#get_int64
) that represents the number of milliseconds since the epoch.
Then use Time::at
to convert that integer to a Time
object
date_time = DateTime.new(2021,8,30)
date_time.to_time
#=> 2021-08-30 00:00:00 +0000
date_time.to_time.to_i
#=> 1630281600
timestamp = BSON::Timestamp.from_bson(date_time.to_bson)
#=> #<BSON::Timestamp:0x00007fffe31da4a8 @seconds=379, @increment=2488994816>
timestamp.to_bson.get_int64 / 1000
#=> 1630281600
Time.at(timestamp.to_bson.get_int64 / 1000).utc
#=> 2021-08-30 00:00:00 UTC
convert time DD:hh:mm:ss format into seconds in Rails 3
You didn't state which version of ruby you were using, so I'm assuming something modern. This should work on 1.9.3 or later. This also assumes that you always have all 4 fields present. It should give you some ideas anyway.
> db_value = '456:14:56:10'
=> "456:14:56:10"
> units = %w{days hours minutes seconds}
=> ["days", "hours", "minutes", "seconds"]
> db_value.split(":").map.with_index{|x,i| x.to_i.send(units[i])}.reduce(:+).to_i
=> 39452170
How to convert a unix timestamp (seconds since epoch) to Ruby DateTime?
DateTime.strptime
can handle seconds since epoch. The number must be converted to a string:
require 'date'
DateTime.strptime("1318996912",'%s')
Collecting and converting date field for an array
Todo.all.map { |todo| todo.date.strftime("%m/%d/%y") }
Related Topics
Xmlhttprequest Status 0 (Responsetext Is Empty)
Change Second Select List Based on First Select List Value in Rails
How to Display PDF (Blob) on iOS Sent from My Angularjs App
Get Word Under Tap from Uiwebview Using JavaScript
JavaScript Search Array of Arrays
Js.Erb Not Executing JavaScript But Is Processed Rails
JavaScript Double Colon (Bind Operator)
Get Gps Location from the Web Browser
How to Split a Long Regular Expression into Multiple Lines in JavaScript
How to Write a Script to Edit a JSON File
Shiny App:Disable Downloadbutton
Rails Specify Load Order of JavaScript Files
Passing Variable from JavaScript to Ruby on Rails
Jqgrid, How to Make a Column Editable in the Add Dialog But Not During (Inline) Edits
Set a Request Header in JavaScript
The Definitive Best Way to Preload Images Using JavaScript/Jquery
Twitter Bootstrap Rails Button Dropdown No Responding to Ajax