How do I get day of the week from a date object? - ruby

I have a date object in Rails, which I'd like to format.
I've made it this far:
delivery_time.date.strftime("%w, %d/%m/%Y")
I'd like it to print out 'Wednesday, 04/01/2012'
Is there a quick method to change the '%w' into 'Wednesday'?

Looking at the Ruby docs for strftime
Time.now.strftime("%A, %d/%m/%Y")
=> "Wednesday, 04/01/2012"
The %A character is the full day name.

Date.today.strftime("%A")
=> "Wednesday"
Date.today.strftime("%A").downcase
=> "wednesday"

I know the OP wanted the string for the weekday, but if for whatever reason you are doing something more algorithmic and need a numerical representation, you can use cwday on the Date object
d = Date.new(2017,7,26)
d.cwday
#=> 3
Monday is 1

%A gives you day of the week.
strftime("%A, %d-%m-%Y") will give you:
Wednesday, 04-01-2012

Yes, you can use "%A" for the full day name.
See also http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html

try %A instead of %w
delivery_time.date.strftime("%A, %d/%m/%Y")

Related

Creating an array of something by not today's date?

I have two arrays I'm trying to make. The first one is all the "quotes" with a "follow_up_date" that is today's date. My code looks like:
#dailyTasks = Quote.where(:follow_up => 1,:follow_up_date => Date.today())
This works perfectly for me and is not the issue.
My next array is the same concept but all the quotes to be added are quotes with a follow_up_date that is not today's date. Something like this is what I'm looking for:
#upcomingTasks = Quote.where(:follow_up => 1, :follow_up_date => NOT Date.today())
Does anyone know the proper syntax I should be using to add quotes whose date is NOT today's date?
You can use where.not to get that negation:
#upcomingTasks = Quote.where(follow_up: 1).where.not(follow_up_date: Date.today())
You can simply use equality operators
today = Date.today
today == some_other_date
Can also use < and > to check if date is earlier or later than another.
today < some_other_date
If using Date.today, it does not compare the exact time like it would if using Time.now from other time/date related built-ins.

Changing the format from a mySQL datetime format to a different type in ruby?

I am attempting to change an SQL datetime variable (2016-06-09 14:29:34) into a format that looks like this (00:00_20160601). I have tried to follow a couple of SO questions that will allow me to format a Time object.
This is what I have done so far:
start_datetime = "2016-06-09 14:29:34"
t =Time.new(start_datetime)
t.strftime("%H:%M_%Y%d%m")
This results in the time being formatted to 2016-01-01 00:00:00 +0000, which is obviously not what I want. I was wondering if someone could help me format the datetime object the way I specified?
You can do this with DateTime:
require 'datetime'
DateTime.parse("2016-06-09 14:29:34").strftime("%H:%M_%Y%d%m")
#=> "14:29_20160906"
The format you're feeding in is basically ISO-8601 so it's parsed by default.
Feeding that value into Time.new is completely incorrect. The first argument there is the year, the rest have to be supplied separately. That's why you get 2016-01-01, since everything else comes out as defaults.
Time.new is converting automatically and the result of "2016-06-09 14:29:34".to_i is 2016.
It's not entirely clear why your day value changes from 09 in the input to 01 in the desired output, so I'll use the normal thing and output the same as was input:
require 'time'
start_datetime = "2016-06-09 14:29:34"
t = Time.strptime(start_datetime, '%Y-%m-%d %H:%M:%S')
t.strftime('00:00_%Y%m%d') # => "00:00_20160609"
Since the hours and minutes are being thrown away there are a couple of other ways to go about this.
Ignore the hours and minutes when parsing:
t = Time.strptime(start_datetime, '%Y-%m-%d')
Or use a Date object instead of a Time object:
require 'date'
start_datetime = "2016-06-09 14:29:34"
t = Date.strptime(start_datetime, '%Y-%m-%d')
t.strftime('00:00_%Y%m%d') # => "00:00_20160609"

Ruby: How to extract an hour (or day) from a date-time string

I'm pulling date-time strings from a large CSV file, which look like this:
"11/19/2008 21:56"
I'd like to extract the hour only, so I can build a histogram of all the hours to find the most frequent ones. Similarly, I'd like to extract days of the week (names) from the dates and build a histogram of most frequent days.
I'm new to Ruby, looked up the information, for starters tried various forms of the following, but no luck:
require 'date'
puts DateTime.strptime("11/19/2008 21:56", '%I')
Can you please advise a simple (and clear) way to accomplish the above? Also, any suggestions how to represent the results would be great. I'm thinking one hash array for the hours (24 entries) and one for the days (7 entries)? What would be the neatest algorithm to load them up as I iterate through the date-time strings, and then maybe re-sorting them with most frequent on top? Thanks!!
This is the starting point:
dt = "11/19/2008 21:56"
require 'date'
DateTime.strptime(dt, '%m/%d/%Y %H:%M') # => #<DateTime: 2008-11-19T21:56:00+00:00 ((2454790j,78960s,0n),+0s,2299161j)>
Date formats like "11/19/2008" present a problem when parsing because the default is to use this format:
'%d/%m/%Y'
Date blows up when it sees a month value of 19. '%m/%d/%Y' is not as popular around the world as '%d/%m/%Y', which is why Ruby defaults to it.
Once you have the timestamp parsed, you can easily extract parts from it:
datetime = DateTime.strptime(dt, '%m/%d/%Y %H:%M')
datetime.hour # => 21
datetime.wday # => 3
Notice that wday returns values from 0..6, not 1..7, where 0 = Sunday:
%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday][datetime.wday]
# => "Wednesday"
Rails' ActiveSupport has a lot of useful methods as part of its Date, DateTime and Time support. Using them is easy, and it's easy to cherry-pick which you want if you decide to add them to plain-ol' Ruby code.
"11/19/2008 21:56".split[1]
=> "21:56"
If can be in other formats, but always the only part with a ":" and two digits on each side, you can use
"11/19/2008 21:56"[/\d{2}:\d{2}/]
=> "21:56"
And for day, something similar
"11/19/2008 21:56"[/\d{2}\/\d{2}\/\d{4}/]
=> "11/19/2008"

How to split the string obtained from "Time.now" in ruby

A=Time.now
gives following output:
2012:05:18 12:20:50 +0530
I want to form a string like "May 18 , 2012". I tried changing A to string which was not possible. How can I achieve this?
Time.now.strftime('%b %e , %Y')
More info.
You can have a look at the Time Class, and method strftime.
It formats time according to the directives in the given format string.
http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime
For your current problem:
Time.now.strftime("%b %d, %Y")

How do I format a date to mm/dd/yyyy in Ruby?

In Perl you can do:
my $current_time = DateTime->now();
my $mdy = $current_time->mdy("/");
What's the easiest way to do this in Ruby?
The strftime method can be used to format times:
Time.now.strftime("%m/%d/%Y")
I wrote a gem to help with formatting dates, and keeping your views DRY (not having to strftime every time you want to format dates).
Check it out at: http://github.com/platform45/easy_dates
You can simply use %D with strftime method
> Time.now.strftime("%D")
=> "11/24/14" # mm/dd/yy
my $current_time = DateTime->now();
my_current_time = DateTime.now
my $mdy = $current_time->mdy("/");
my_mdy = my_current_time.strftime("%m/%d/%Y")

Resources