Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

endOfMonth(): determining the last day of any given monh


  • Please log in to reply
9 replies to this topic
Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005
I'm not sure whether something like this already exists, and there's probably an easier way to do it - but here's a little function I wrote to determine the last day of any given month based on a date within that month (e.g. from June 2 you'd get June 30):
endOfMonth(date)
{
	; get current month and year
	FormatTime, date, %date%, yyyyMM
	StringRight, month, date, 2
	FormatTime, year, %date%, yyyy
	; select first day of the following month
	If month < 12
	{
		month++
		If month < 10 ; add leading zero
			month = 0%month%
		date = %year%%month%01
	}
	Else
	{
		year++
		date = %year%0101
	}
	; select last day of actual month
	date += -1, Days
	; return date
	Return date
}
I've run a few tests and it seems to work fine - still, if you find any bugs, please let me know.


PS: If you can think of a better (more telling) name for this function, please let me know; proper naming of functions and variables is very important to me...

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Ace_NoOne, :)

See Laszlo's LDOM() Last Day Of Month.

Regards :)
kWo4Lk1.png

Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005
Hmpf! :p

That solution is definitely smarter than mine. (And I was so proud of my solution of taking the first day of the next month and just substracting 1 day... :oops: )

Joy2DWorld
  • Members
  • 562 posts
  • Last active: Jun 30 2014 07:48 PM
  • Joined: 04 Dec 2006
LDOM(TimeStr="") { 
  If TimeStr= 
     TimeStr = %A_Now% 
  StringLeft Date,TimeStr,6 ; YearMonth 
  Date1 = %Date% 
  Date1+= 31,D              ; A day in next month 
  StringLeft Date1,Date1,6  ; YearNextmonth 
  Date1-= %Date%,D          ; Difference in days 
  Return Date1 
}


cool.... but what happens if I check on Jan 30 or 31st ?

Jan 30+31d -> March....


hmmmm....


either i'm missing something (very likely)


or...



Ace_NoOne... you solution is actually smarter!



and with Laszlo's often artful and dare is speculate, Genius code...

you aught to be darned proud!
Joyce Jamce

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

what happens if I check on Jan 30 or 31st ?

The LDOM function keeps only the first 6 digits of the date, that is the year and the month. Without the day field, a date value defaults to the first day of the month, so when we add 31 days, it is always in the next month. In case of any day input in January, we get February the first, in case of any day in February, we get March 3rd, or 4th etc. (This trick saves a concatenation of "01")

Then we delete the days form the result, getting the first day of the next month, implicitly. The difference of the two truncated dates gives the number of days in the current month, which is also the number of the last day in this month. It is just shorter this way, leaving the hard work to the built in date functions.

Joy2DWorld
  • Members
  • 562 posts
  • Last active: Jun 30 2014 07:48 PM
  • Joined: 04 Dec 2006
indeed...




you strip the days... add 31 to the 1st...



brilliant....



(sorry, Ace_NoOne, Laszlo's *is* smarter...)
Joyce Jamce

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Well, I have already earned the smart dude title. 8)

Joy2DWorld
  • Members
  • 562 posts
  • Last active: Jun 30 2014 07:48 PM
  • Joined: 04 Dec 2006
i wish i had known that *BEFORE* i posted...

(i would have at least run the script to test it... )

had i know...



(per the cracked-nut post:)

"smartest dude in the world"
Joyce Jamce

daveand5
  • Members
  • 20 posts
  • Last active: Feb 10 2009 06:11 PM
  • Joined: 09 Nov 2007
Sort of.....
the line :
Date1-= %Date%,D ; Difference in days
the ,D ?? is a time or format option?
I played with it for a few minutes, and understand its defaulting to the 1st day of each month, and the D is causing this?, as remove it and you get 1
I searched for formating options, etc and find no reference to this ,D
what other format /time options are available M,m,Y,y?

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

I searched for formating options, etc and find no reference to this ,D


Refer AHK doc for EnvAdd command..
.. and yes, it is misleading.

:)