Jump to content

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

How to retrieve the last day of the current month


  • Please log in to reply
4 replies to this topic
Andi
  • Members
  • 195 posts
  • Last active: Apr 18 2014 05:03 PM
  • Joined: 11 Feb 2005
I want to retrieve the date of the last day of the current month.

e.g.
31.01.05
28.02.05
31.03.05
etc.

Perhaps one could take the first day of the following month and subtract one day, but I don't know how to do this? Has someone a tip for me?

daonlyfreez
  • Members
  • 995 posts
  • Last active: Jan 23 2013 08:16 AM
  • Joined: 16 Mar 2005
lastDayOfThisMonth =

30daysMonths = 04,06,09,11

leapYears = 08,12,16,20

;

formattime, currentMonth,%a_now%, MM

formattime, currentYear,%a_now%, YY

;

IfEqual, currentMonth, 02

{	

	if currentYear in %leapYears%

	{

		lastDayOfThisMonth = 29

	}

	else

	{

		lastDayOfThisMonth = 28

	}

}

else

{

	if currentmonth in %30daysMonths%

	{

		lastDayOfThisMonth = 30

	}

	else

	{

		lastDayOfThisMonth =31

	}

}

msgbox, The last day of this month is the %lastDayOfThisMonth%(th/st)

Posted Image mirror 1mirror 2mirror 3ahk4.me • PM or Posted Image

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Good solution. I like date challenges so here's another one that seems correct:
if A_MM = 12

{

	NextMonth = 01

	Year := A_YYYY + 1

}

else

{

	NextMonth := A_MM + 1

	NextMonth = 0%NextMonth%  ; Add leading zero.

	StringRight, NextMonth, NextMonth, 2   ; Remove inappropriate leading zero, if any.

	Year := A_YYYY

}

FirstDayOfNextMonth = %Year%%NextMonth%01

FirstDayOfNextMonth += -1, day  ; Subtract one day to yield last day of this month.

StringMid, LastDay, FirstDayOfNextMonth, 7, 2  ; Extract just the day.

MsgBox Last day of this month: %LastDay%


daonlyfreez
  • Members
  • 995 posts
  • Last active: Jan 23 2013 08:16 AM
  • Joined: 16 Mar 2005
:) Wow, I didn't know you could do direct math on the datestring and even without some sort of identifier... 8)
Posted Image mirror 1mirror 2mirror 3ahk4.me • PM or Posted Image

Andi
  • Members
  • 195 posts
  • Last active: Apr 18 2014 05:03 PM
  • Joined: 11 Feb 2005
That's the answer to this problem. :D

Thank you!!!!!!