Rails DateTime.now without Time - ruby-on-rails

I need to use DateTime.now to grab the current date, and "strip off" the time.
For example, this shows what I don't want: DateTime.now => Sat, 19 Nov 2011 18:54:13 UTC +00:00
This shows what I do want: DateTime.now.some_operation => 2011-11-06 00:00:00 UTC

You can use one of the following:
DateTime.current.midnight
DateTime.current.beginning_of_day
DateTime.current.to_date

What you need is the function strftime:
Time.now.strftime("%Y-%d-%m %H:%M:%S %Z")

You can use just:
Date.current

What about Date.today.to_time?

If you're happy to require 'active_support/core_ext', then you can use
DateTime.now.midnight # => Sat, 19 Nov 2011 00:00:00 -0800

If you want today's date without the time, just use Date.today

Figured it out. This works:
DateTime.now.in_time_zone.midnight

Related

How do I get yesterday's time in milliseconds?

I'm running into a wall. I want to get yesterday's time in milliseconds. I thought the below would do the job
irb(main):001:0> yday = 1.day.ago
=> Tue, 03 Apr 2018 20:35:24 UTC +00:00
irb(main):002:0> yday.strftime('%Q').to_f
=> 0.0
but as you can see I'm getting a "0.0" for yesterday's time. How can I correct what I have to accurately get the time in milliseconds?
You have two options:
(DateTime.now-1.day).strftime('%Q')
Or:
yday = 1.day.ago
yday.to_datetime.strftime('%Q')
Why you need to_datetime? Well, 1.day.ago is ActiveSupport::TimeWithZone which does not have %Q format (documentation) when strftime for DateTime does (documentation)

Convert Active Support timezone original format into a string

I'm trying to convert Active Support timezone original format into a string. I want to store it in an array of characters then parse each needed data individually.
Time.zone = current_user.timezone
date_and_time = Time.zone.now
Now
date_and_time = Thu, 21 Apr 2016 20:58:04 PDT -07:00
Ruby method ( to_s ) does not convert it. I found other ways to convert it to but all of them will change the format to numbers only, I want the day to stay the same because I will store it in a variable then use it in a different method.
You can use .to_formatted_s(DATE_FORMAT) for this.
time = Time.now # => Thu Jan 18 06:10:17 CST 2007
time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
time.to_formatted_s(:long) # => "January 18, 2007 06:10"
time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
time.to_formatted_s(:iso8601) # => "2007-01-18T06:10:17-06:00"
A list of all DATE_FORMATS and more information can be found here:
http://api.rubyonrails.org/classes/Time.html#method-i-to_formatted_s
You can try this
date_and_time.strftime("%a %d %b %Y")
Also You can check this guide, to get format you want
You should get what you want using this :
date_and_time.strftime("%a %d %b %Y %H:%M:%S UTC %:z")
Please see strftime Docs for more info
Explanation
Reason for hardcoding UTC is so that according to the docs
%z - Time zone as hour and minute offset from UTC
So i believe it should be UTC all the time.

Rails - A better way to parse the time with a timezone?

Time.parse returns a Time object that does not have a timezone. I would like to keep the timezone information. Is there a better way to do this then the following code?
def parse_with_timezone( string_input)
/(.*)([+-]\d\d):?(\d\d)$/.match( string_input) do |match|
tz = ActiveSupport::TimeZone[match[2].to_i.hours + match[3].to_i.minutes]
tz.parse( match[1])
end
end
The input is a string like this "2012-12-25T00:00:00+09:00". This function outputs a TimeWithZone object.
Were you looking for a specific timezone of the current local one?
# Current zone
1.9.3p194> Time.zone.parse('2012-12-25T00:00:00+09:00')
=> Mon, 24 Dec 2012 15:00:00 UTC +00:00
Console was set at UTC for above but will work for whatever you have configured
# Specific timezone
1.9.3p194> Time.find_zone('Wellington').parse('2012-12-25T00:00:00+09:00')
=> Tue, 25 Dec 2012 04:00:00 NZDT +13:00
I notice you're trying to pass +9 so as an example
1.9.3p194> Time.zone = 'Tokyo'
=> "Tokyo"
1.9.3p194> Time.zone.parse('2012-12-25T00:00:00+09:00')
=> Tue, 25 Dec 2012 00:00:00 JST +09:00
Gives you the right result.
What about the Rails Timezone API: http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html
I prefer to use Chronic for all my date/time parsing needs.

Ruby String to_time broken?

I would expect Time and to_time to be reflective.
UPDATE
This is the answer, if you add the parameter :local, it doesn't ignore the offset. I find it wierd that it ignores data (the offset) by default, though...
Time.zone.now.to_s.to_time(:local) returns 2012-03-20 14:39:03 +0100
/UPDATE
> Time.zone.now.to_s
=> "2012-03-20 12:50:05 +0100"
> Time.zone.now.to_s.to_time.to_s
=> "2012-03-20 12:50:05 UTC"
# PROBLEM:
# UTC is +0000, meaning that there is 1 hour diff in the above
> Time.zone.now
=> Tue, 20 Mar 2012 12:51:32 CET +01:00
> Time.zone.now.to_time
=> Tue, 20 Mar 2012 12:51:32 CET +01:00
> Time.zone.now.to_json
=> "\"2012-03-20T12:50:36+01:00\""
> Time.zone.now.to_json.to_time
=> 2012-03-20 12:50:36 UTC
I have this problem with JSON messages. Anyway, I wouldn't expect to_time to actually change the time. OK to translate to UTC, probably, but it should adjust time accordingly. Anybody know if this is a known issue or "by design"?
This method, String#to_time, defined in ActiveSupport takes one parameter, form, which can be either :utc (default) or :local. So by default, it always returns a Time in UTC.
To get a Time with timezone:
Time.zone.parse("2012-03-20T12:50:36+01:00")

ROR + TodayDate in Different Format

If I have #today = Date.today.to_s, how do I convert #today into UTC (with the appropriate date only)? But the format should be like this : 2011-03-08 00:00:00
Acutally I am looking for Yesterday date also ??
This worked for me to get yesterday at time 00hr
Date.yesterday.to_time.utc.at_beginning_of_day
=> Sun Mar 06 00:00:00 UTC 2011
I don't know if its the correct way to do it but it works
ruby-1.9.2-p0 > Date.today.to_time.utc
=> 2011-03-07 18:15:00 UTC
and for yesterday, you can subtract a day form today
(Date.today-1.day).to_time.utc
=> 2011-03-06 18:15:00 UTC
For Yesterday Date.
#date = Date.yesterday
=> Mon, 07 Mar 2011
#date.to_time.utc.strftime("%Y-%m-%d %H:%M:%S")
=> "2011-03-06 18:30:00"
This will do(%F - %Y-%m-%d and %T - %H:%M:%S)
Date.today.strftime("%F %T")
Date.yesterday.strftime("%F %T")
If you are particular about UTC time, you should do like this
(Time.now.utc).to_date.strftime("%F %T")
(Time.now.utc - 1.day).to_date.strftime("%F %T")

Resources