Groovy/Grails date class - getting day of month - grails

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)

Related

Add multiple number of months to specific date until date in future - Google Sheets

In Google Sheets I need calculate next date in future from TODAY based on start date adding specific number of months (or multiple of them). When I will open it everytime in future it will always calculate correct date.
Example:
Today is 2021-12-01
Start date = 2021-02-07, interval = 5 months => next date should be 2021-12-07
Start date = 2021-09-07, interval = 3 months => next date should be 2021-12-07
Start date = 2021-09-07, interval = 12 months => next date should be 2022-12-07
Start date = 2021-12-07, interval = 12 months => next date should be 2021-12-07
Idealy would be to have 3 cells: start date, interval, future date.
I have no idea how to do it using one "unversal" formula.
Please help.
=DATE(YEAR(A1),(MONTH(A1)+B1),DAY(A1))
Where:
A1 = Start date
B1 = Interval
C1 = Formula (Next date)

How to add a number to the date ruby

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

How to create new Date instance only by a day

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)

How to get date of particular week in Rails

I am working on Rails application where I am trying to fetch date of particular week but week start from not 1 January of year but some fixed date.
Like, my week start from 8 july 2016 (08-07-2016) so now i want to fetch start date and end date of any week.
Means week 1 start date -> 08-07-2016 and end date -> 14-07-2016.
Now i want to fetch any week start date and end date but how? I already tried but got solution of year start date not like this.
Any one have idea?
Thanks in advance.
Ok friends,
I found my answer below
week = 3
number = (week-1) * 7
start_date = Date.new(2016, 7, 8) +number.to_i
d = start_date.to_s.split('-')
end_date = Date.new(d[0].to_i, d[1].to_i, d[2].to_i) + 6
end_date = end_date.strftime("%Y-%m-%d")
Let me know if any one have confusion in this answer.
There is no need to split the start date and create a new date object.
start_date_of_first_week = Date.new(2016, 7, 8)
week_number = 3
start_date = start_date_of_first_week + ( (week_number - 1) * 7 )
end_date = start_date + 6

How to get date from string with this format yyyy-MM-dd'T'HH:mm:ss.SSSZZZ in lua script

How to get os.time from the data string with this format yyyy-MM-dd'T'HH:mm:ss.SSSZZZ and I will do some calculations to get date after number of days from the above date, but I need to get the resultant date also in this format yyyy-MM-dd'T'HH:mm:ss.SSSZZZ
I am able to do the calculations but the only issue is in need to read the date with the above format and return the date in the same format.
now = os.time{year = yearValue, month = monthValue, day = dayValue, hour = hourValue, min = minutesValue}
numberOfDays = now + 2 * 24 * 3600 -- here 2 is the number of days
print(os.date("%c",numberOfDays)) -- number of days returns the date after give number of days
Can anyone help me with the date format(yyyy-MM-dd'T'HH:mm:ss.SSSZZZ) that i should read and return. Thanks in advance
print(os.date("%Y-%m-%dT%H:%m:%S.000 %z",numberOfDays))
I think this is the closest you can get. milliseconds is hardcoded 000 because there are no milliseconds in lua and the timezone is like +0100 for example.
reference to date formatting used in lua: http://www.cplusplus.com/reference/ctime/strftime/

Resources