Skip to content

Instantly share code, notes, and snippets.

@NatashaTheRobot
Created December 12, 2011 19:10
Show Gist options
  • Save NatashaTheRobot/1468614 to your computer and use it in GitHub Desktop.
Save NatashaTheRobot/1468614 to your computer and use it in GitHub Desktop.
Format Time To Today At Midnight GMT In Ruby
require 'time'
#convert today's time to a string
today = Time.now.to_s # => "Mon Dec 12 10:52:45 -0800 2011"
#replace the hours:minutes:seconds and time-zone to the time and time zone that you need
today[11, 14] = "00:00:00 +0000" # => "Mon Dec 12 00:00:00 +0000 2011"
#convert the time string back into time format
Time.parse(today) # => Sun Dec 11 16:00:00 -0800 2011 - The time is adjusted for my time-zone (-0800), but it is equivalent to midnight in GMT time (Mon Dec 12 00:00:00 +0000 2011)
#if you need to, convert the time into a UTC
Time.parse(today).strftime("%s").to_i # => 1323648000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment