AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Ace_NoOne



Joined: 10 Oct 2005
Posts: 299
Location: Germany

PostPosted: Fri Jun 02, 2006 11:22 am    Post subject: endOfMonth(): determining the last day of any given monh Reply with quote

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):
Code:
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...
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Fri Jun 02, 2006 11:32 am    Post subject: Reply with quote

Dear Ace_NoOne, Smile

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

Regards Smile
_________________
URLGet - Internet Explorer based Downloader
Back to top
View user's profile Send private message Send e-mail
Ace_NoOne



Joined: 10 Oct 2005
Posts: 299
Location: Germany

PostPosted: Fri Jun 02, 2006 12:59 pm    Post subject: Reply with quote

Hmpf! Razz

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... Embarassed )
Back to top
View user's profile Send private message
Joy2DWorld



Joined: 04 Dec 2006
Posts: 561
Location: Galil, Israel

PostPosted: Sat Jan 06, 2007 11:45 pm    Post subject: Maybe i not enough sleep... but it seems that... Reply with quote

Code:
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
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Sun Jan 07, 2007 1:01 am    Post subject: Reply with quote

Joy2DWorld wrote:
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.
Back to top
View user's profile Send private message
Joy2DWorld



Joined: 04 Dec 2006
Posts: 561
Location: Galil, Israel

PostPosted: Sun Jan 07, 2007 2:01 am    Post subject: Reply with quote

indeed...




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



brilliant....



(sorry, Ace_NoOne, Laszlo's *is* smarter...)
_________________
Joyce Jamce
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Sun Jan 07, 2007 2:48 am    Post subject: Reply with quote

Well, I have already earned the smart dude title. Cool
Back to top
View user's profile Send private message
Joy2DWorld



Joined: 04 Dec 2006
Posts: 561
Location: Galil, Israel

PostPosted: Sun Jan 07, 2007 1:23 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
daveand5



Joined: 09 Nov 2007
Posts: 20
Location: http://taplpoker.com/

PostPosted: Wed Nov 28, 2007 7:53 am    Post subject: I think I follow everything Reply with quote

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?
Back to top
View user's profile Send private message Send e-mail MSN Messenger
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Wed Nov 28, 2007 8:02 am    Post subject: Re: I think I follow everything Reply with quote

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


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

Smile
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group