Getting last day of current month - groovy

In Groovy, how can I find the last day of the current month?
Thanks for your sage advice and better wisdom.

Yeah, as you commented, Calendar.FIELD is probably your best friend (though groovy's TimeCategory also has neat tricks).
Here's how i made for the last day of the current month:
Calendar.with {
println instance.getActualMaximum( DATE )
}
I didn't understood what you meant by "break out the current weekday if I'm looping through a month". Perhaps getting the weekday for each month?
Calendar.with {
(JANUARY..DECEMBER).each { month ->
def cal = instance
cal[MONTH] = month
println cal[DAY_OF_WEEK]
}
}

Related

Why does new Date(year,month,day) not return an equivialent Date?

one day I fiddled with vanilla NodeJS using the node command line tool. (I am using node v13.11.0)
I tried to create a new Date at the 01.01.1970. I used the usual new Date(year, month, day) constructor.
As simple as it sounds, I entered new Date(1970, 1, 1) and found out, that it does not return 1970-01-01T00:00:00.0000Z. Instead, it returns 1970-01-31T12:00:00.000Z.
Has anyone an Idea, why this constructor does not return the equivalent date?
The constructor does more or less what you think:
x = new Date(1970,1,1)
1970-01-31T14:00:00.000Z
> x.getMonth()
1
> x.getDate()
1
> x.getHours()
0
(Note that months count from zero, so you requested the 1st of February).
But if you display the whole date as a string, it's showing the time in UTC, which might not be what you expect.

Moment.js sets dates to 1 day behind

I am using 2.22.1 to format dates.
When i put in a date that comes from a date selector, moment will put the date back one day. For example, when I try to format a date in the following way:
Example 1:
const day = new Date(value).getDate();
const month = new Date(value).getMonth();
const fullYear = new Date(value).getFullYear();
console.log('day', day); // 7
console.log('month', month); // 5
console.log('fullYear', fullYear); //2018
Formatting function:
moment.utc(new Date(month + ' ' + day + ' ' + fullYear), 'MM-DD-YYYY').format('DD-MM-YY')
Output: 06-05-18
Expected: 07-05-18
Example 2:
Input: Thu May 31 2018 00:00:00 GMT+0100 (GMT Summer Time)
Formatting function:
moment.utc(new Date(value)).format('DD-MM-YY')
Output: 30-05-18
Expected: 31-05-18
moment.utc will convert the input to universal time.
You are running new Date(...) with an input that's based on GMT +1.
If you think about this, it makes total sense.
Your output is 30-05-18, because it's 11 PM / 23:00 o'clock on the previous day.
This (how ever) would in fact work:
moment('05-06-2018', 'MM-DD-YYYY').format('DD-MM-YY')
// alternatively (and preferrably) this:
moment.utc('05-06-2018', 'MM-DD-YYYY').format('DD-MM-YY')
and output: "06-05-18"
Because the non utc version does not take a time input in this example.
One of the reasons moment.js exists, is to get rid of Date in your code all together.
(Keep in mind tho, that Date has drastically improved now. DateTimeFormat is a game changer)
Please just read the momentjs documentation on how to properly use moment.
edit:
If you want to process 400000 dates with this, I'd advise using RegExp, .split, .exec, .slice or Date instead.
(I can relate since I wrote a client sided Log parser with javascript generators and Service Workers for a statistical anti-cheat analysis)
I truly recommend playing around with such things to raise your knowledge.
I just ran into this issue and a quick fix I found for the time being processed in "Zulu time" (due to the Z at the end of the string) is to add .LocaleString() after the date variable.
I find that for data consistency, it's easier to store data in UTC time and then convert it to the locale string when displaying the data to the user.
For example, I'm using:
moment.utc(dateVariable.toLocaleString()).format("MM/DD/YYYY")

Manipulation of date in Groovy language

I have this script in Groovy:
currentDate = new Date().format( 'yyyyMMdd' )
I want to be able to manipulate the date in order to 'play' with the dates of it..
for example if I have this:20150701 I want to subtract days, weeks or months for example if I subtract one day it will be 20150630.
How can I do it without using TimeCategory?
thanks!
Without TimeCategory, you can only add or subtract days. If you want to add/subtract other fields, TimeCategory is a good way to go.
If your annoyance with TimeCategory is the with syntax, one alternative would be to use mixins (although they are generally considered deprecated since traits have been added to Groovy):
[Date, Integer].each { it.mixin(groovy.time.TimeCategory) }
def lastMonth = new Date() - 1.months
OK I have the answer to it, I tried this and it worked:
currentDate = new Date()
def yesterday = currentDate - 1
currentDate = yesterday.format("yyyyMMdd")

How to calculate exact year, month and day between two dates in vc++/MFC

In my application i have to find out exact year, month and day between two dates. I tried with below snip. but answer is not correct. I used CTime for from to to dates. The result is obtained in CTimeSpan class. CTimeSpan has member function such as GetDays. We shall find out year, month and day from Total Days, but answer is not correct. Is any way to do ?
Thanks in advance
There is no common function to do that.
My idea would be:
COleDateTime date1(1979,12,31,0,0,0),
date2(2004,10,01,0,0,0);
// Get the normal differences
int iYears = date2.GetYear()-date1.GetYear(),
iMonths = date2.GetMonth()-date1.GetMonth(),
iDays = date2.GetDay()-date1.GetDay();
// Problematic underflow of days.
if (iDays<0)
{
// One month less
--iMonths;
// Advance from the start date until we reach the 1st of next month
for (iDays=0; (date1+COleDateTimeSpan(iDays,0,0,0)).GetDay()!=1; ++iDays)
;
// Now get the days from the 1st of the second date to the desired date.
iDays += static_cast<int>((COleDateTime(date2.GetYear(),date2.GetMonth(),1,0,0,0)-date2).GetTotalDays());
}
// underflow of months
if (iMonths<0)
{
--iYears;
iMonths +=12;
}
I don't see any CTimeSpan::GetTotalDays() function in the MSDN Docs.
Anyway, the GetTotalHours / GetTotalMinutes functions return the number of Complete Hours/Minutes and does not fully represent the TimeSpan value; the best way would be to use GetTotalSeconds and convert it as necessary.

How to format a duration in years and months?

I'm using node.js w/ moment.js for formatting times. I want to get a patient's age formatted in months and years. Here's what I've got so far:
patient: {
...
birthDate: moment('Dec 15, 2009'),
getAgeString: function() {
var age = moment.duration(moment() - this.birthDate);
return util.format('%d years, %d months', age.years(), age.months());
}
}
The getAgeString function gives me back 3 years, 1 months, which is pretty close to what I want, except I'd like it to be pluralized properly. As far as I can tell, moment doesn't offer proper pluralization for durations.
this.birthDate.fromNow(true) is "smart" but it doesn't seem to offer any customization options as to what gets displayed.
Can I get moment.js to do what I want, or is there a better time library for node?
This will have to do for now:
getAgeString: function() {
var age = moment.duration(moment() - this.birthDate);
var years = age.years(), months = age.months();
return util.format('%d year%s, %d month%s', years, years === 1 ? '' : 's', months, months === 1 ? '' : 's');
}
The words that you are looking to pluralize properly appear to be string literals in your own code, and not based on the formatting module. You could do a conditional age.years() or age.months() are equal to 1. If they are, use the strings "year" or "month", otherwise use "years" or "months".

Resources