In my rails project I want to get a Date instance only by a day. Year and month are used current value.
I could write like these:
day = 3
date = Date.new(Date.current.year, Date.current.month, day)
and
date = Date.current.beginning_of_month + (day - 1).days
How would you write function like this?
Is there a better implementation?
If you already have a Date object, you can do:
date.change(day: 3)
where date is a Date or DateTime object. Also you can do:
Date.today.change(day: 3)
Related
im having a problem with datetime in rails. Im reading a json and saving dates in a model. that has a column t.date:datetime
The thing is there is 2 dates that i get from this json, and i want only to save it as 1.
I get date for day : 2022-04-16 00:00:00 and also i get a hour date : 01:00:00 is there a way to add that hour date to my day date ? Like getting 2022-04-16 01:00:00 ?
Im new to rails so sorry if im not explaining correctly.
An hour is 1/24th of a day. DateTime happily let's you add that to a date.
t1 = DateTime.today
t2 = t1 + 1/24r # a rational number. 1.0/24 is fine too.
You can convert your time to second and then add it to your date
date = "2022-04-16 00:00:00".to_datetime
time = Time.parse("01:00:00").seconds_since_midnight.seconds
date + time
You don't need to do to_datetimeif your date is already a datetime object
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
Using Rails 4, I have a set of data where I only want the data points where the date is on the last day of the month, any month, but only where the date is the last day of the month.
Below I set up the #data variable all my statistics data limiting it to Statistics where the date is in the last year. I would also like to limit the data to only contain Statistics that have a date of the last day of the month.
EDIT
- Note that the start date is a variable and can be changed by a parameter
startdate = Date.1.year.ago
#data = Statistic.where(date: startdate..Date.today).all
Is there a simple way to add another .where clause and use end_of_month for this?
Write this scope in your Statistics model:
scope :last_date_stats, ->(dates) { where(date: dates) }
Populate a dates array in your StatisticsController and then use the scoped query to get specific data:
dates = (0..11).to_a.map { |n| n.months.ago.end_of_month }
#last_date_statistics = Statistics.last_date_stats(dates)
I'm trying to execute a query in rails 3 with the following syntax: CourseOrder.where("DATE(ordered_at) = ?", date).
My problem is that rails saves all times in UTC, so in my Timezone (+2) at 0:33 its the 4th but in UTC its the 3th of the month. Is there a way to query the date part of a DateTime with Timezone?
Don't use date, do it like this instead:
time = DateTime.civil_from_format(:local, 2011, 4, 9)
CourseOrder.where("ordered_at > ? AND ordered_at < ?", time - 1.day, time)
You're storing a datetime, so query that instead of trying to use date, which doesn't account for Timezones.
solved the problem with a change in which values I compare.
I have a Date value to, and now I'm comparing a date value with a date value which is better than comparing date value with datetime value which have to fail.
thanks for leading me to the right direction
Currently I'm using the following code to get year, month, day of month, hour and minute in Groovy:
Date now = new Date()
Integer year = now.year + 1900
Integer month = now.month + 1
Integer day = now.getAt(Calendar.DAY_OF_MONTH) // inconsistent!
Integer hour = now.hours
Integer minute = now.minutes
// Code that uses year, month, day, hour and minute goes here
Using getAt(Calendar.DAY_OF_MONTH) for the day of the month seems a bit inconsistent in this context. Is there any shorter way to obtain the day of the month?
If you add the following to your code it should assign the day of the month to the day Integer:
Integer day = now.date
Here's a stand-alone example:
def now = Date.parse("yyyy-MM-dd", "2009-09-15")
assert 15 == now.date
Isn't all those lines are kind of garbage?
The following two lines do the job in a groovysh
date = new Date()
date.getAt(Calendar.DAY_OF_MONTH)
To use this at your real code and not in the console
def date = new Date()
def dayOfMonth = date.getAt(Calendar.DAY_OF_MONTH)
The way you are doing it is the only way. The java date object stores the month and day, but doesn't store any information such as how long the given month is or what day of the month your on. You need to use the Calendar class to find out a lot of this info.
You can get just the day of the month from a Date in Groovy like this:
​Date date = new Date()
int dayOfMonth = date[Calendar.DAY_OF_MONTH]
This version is fairly terse:
Calendar.instance.get(Calendar.DAY_OF_MONTH)