Created
December 12, 2011 19:10
-
-
Save NatashaTheRobot/1468614 to your computer and use it in GitHub Desktop.
Format Time To Today At Midnight GMT In Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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