I am working in ruby, i ahave an object containing today's datetime from database. I only want time truncating the date. How can i get that?
Try DateTime#strftime.
DateTime.now.strftime("%H:%M")
# => "12:17"
#!/usr/bin/ruby -w
time1 = Time.now
puts "Current Time : " + time1.hour + ":" + time1.min + ":" + time1.sec
Taken from & credit to - This TutorialsPoint Post.
If you use it really often you can use the gem time_splitter to help you set some accessors of date, time, hour and minute for your datetime attributes. Hope it helps.
You can check the different Rails date formats from here : Rails Date Formats
Example :
time = Time.now
time.strftime("%d %B %Y")
# => 12 December 2016
Related
I'm attempting to retrieve the date which is 24 hours ago.
time = Time.now.to_time - 24.hours
time.to_date
=> Wed, 05 Oct 2022
I require it in the following format:
2022-10-05
Having read the Ruby time docs, and various questions here, I still haven't figured out the must succinct and clean way to go about this.
When you have a Date object it is simply
puts Date.today - 1 # => 2022-10-05
With a Time Object , the - method subtracts seconds. So:
Time.now - 24*60*60
All this works without Rails.
You can rebuild any value with specific date methods :
Time.now.day
=> 6
Time.now.month
=> 10
Time.now.year
=> 2022
So doing something like
formatted_date = "#{time.year}-#{time.month}-#{time.day}"
should work
Also you can check strftime() method documented here : https://apidock.com/ruby/DateTime/strftime. It seems your format is included in ISO8601 formats : time.strftime("%F")
How about this?
Date.yesterday.to_s #=> "2022-10-5"
I have the time of this format "20:10 PM", I need to find the difference between Time.now with the given time
For example If Time.now says 2022-05-17 18:32:52.133290553 -0700, I need to find the difference between Time.now and "20:10 PM" for same-day today.
I am not able to find any references on Ruby on Rails for that.
Very simple way:
>> Time.now - Time.parse("20:10 PM")
=> 10995.706874
Time.parse will assume today's date if it is not given a date. Returned result is in seconds.
Assuming you always want to compare Time.now with a time from today (i.e 20:30 for today), you could construct a new Time instance for the time you're comparing.
todays_year = Time.now.year
todays_month = Time.now.month
todays_day = Time.now.day
time_to_compare = Time.new(todays_year, todays_month, todays_day, 20, 30) # 20 and 30 represent the dynamic time you want to compare
time_diff = ((Time.now - time_to_compare) / 3600).round(2) # assuming you want to know the difference in hours and rounded to 2 decimal places
I have method:
def date_of_next()
date = Date.parse('Monday')
delta = date > Date.today ? 0 : 7
date + delta
return date.strftime('%Y-%w-%d'), (date + 7).strftime('%Y-%w-%d')
end
This method specifies the date of Monday this week.
In return, I also return the date of Monday next week, but it does not display the date that needs to be
if I run this method in the .rb file then it prints everything well
2019-01-28
2019-02-4
but if run this method in rails controller, there is a problem with the date
2019-01-28
2019-01-04
As you see, there is a problem with the date of the month.
I do not understand what the problem is, help
I think you're going a little heavy on your code for a method that returns Monday this Week and Monday next week.
def beginning_of_weeks
monday = Date.new.beginning_of_week
return monday, monday + 7.days
end
You can, of course, add the formatting function strftime('%Y-%m-%d') inside the method, though it would be better organized somewhere else.
Maybe you mixed date format?
def date_of_next()
date = Date.parse('Monday')
delta = date > Date.today ? 0 : 7
date + delta
return date.strftime('%Y-%m-%d'), (date + 7).strftime('%Y-%m-%d')
end
I need to find out the last day of the next month.
I tried but couldn't find out.
Example: Date.today.end_of_month.next_month
Date.today.next_month.end_of_month
Only in Rails or with Active Support:
(Date.today + 1.month).end_of_month
or with time:
1.month.from_now.end_of_month
use
(Date.today + 1.month).end_of_month
I have a DailyQuote model in my rails application which has a date and price for a stock. Data in the database has been captured for this model including weekends. The weekend price values have been set as 0.
I want to change all the weekend prices for Saturday and Sunday to whatever the price was on Friday. What is the best way to do this in Ruby? To identify if a date falls on a Sat or Sun and change its value to the Fri of that weekend?
TFM shows an interesting way to identifying the day of the week:
t = Time.now
t.saturday? #=> returns a boolean value
t.sunday? #=> returns a boolean value
The simplest approach:
today = Date.today
if today.saturday? || today.sunday?
puts "Today is a weekend!"
end
You can also do this for any other day of the week. Ruby is fantastic and offers a lot of cool methods like this. I suggest when you get stumped take a look at what's available to the class by running .methods on it. So if you run Date.today.methods you will see these available.
require 'date'
today = Date.today
ask_price_for = (today.wday == 6) ? today - 1 : (today.wday == 0) ? today - 2 : today
or
require 'date'
today = Date.today
ask_price_for = (today.saturday?) ? today - 1 : (today.sunday?) ? today - 2 : today
ask_price_for now holds a date for which you would want to ask the price for.
Getting the actual price which is corresponding to you date depends on your Model and your ORM-Library (i.e. ActiveRecord).
class Time
def is_weekend?
[0, 6, 7].include?(wday)
end
end
time = Time.new
puts "Current Time : " + time.inspect
puts time.is_weekend?
since rails v5:
Date.current.on_weekend?
References:
api-doc
rails docu
Date.today.instance_eval { saturday? || sunday? }
Checking of weekend days (Saturday and Sunday) in the range of two dates in ruby
weekend_days = [0,6]
if (start_date.to_date..end_date.to_date).to_a.select {|k| weekend_days.include?(k.wday)}.present?
# you code
end