| View previous topic :: View next topic |
| Author |
Message |
Ace_NoOne
Joined: 10 Oct 2005 Posts: 299 Location: Germany
|
Posted: Fri Jun 02, 2006 11:22 am Post subject: endOfMonth(): determining the last day of any given monh |
|
|
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 |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Fri Jun 02, 2006 11:32 am Post subject: |
|
|
Dear Ace_NoOne,
See Laszlo's LDOM() Last Day Of Month.
Regards  _________________ URLGet - Internet Explorer based Downloader |
|
| Back to top |
|
 |
Ace_NoOne
Joined: 10 Oct 2005 Posts: 299 Location: Germany
|
Posted: Fri Jun 02, 2006 12:59 pm Post subject: |
|
|
Hmpf!
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... ) |
|
| Back to top |
|
 |
Joy2DWorld
Joined: 04 Dec 2006 Posts: 561 Location: Galil, Israel
|
Posted: Sat Jan 06, 2007 11:45 pm Post subject: Maybe i not enough sleep... but it seems that... |
|
|
| 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 |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Sun Jan 07, 2007 1:01 am Post subject: |
|
|
| 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 |
|
 |
Joy2DWorld
Joined: 04 Dec 2006 Posts: 561 Location: Galil, Israel
|
Posted: Sun Jan 07, 2007 2:01 am Post subject: |
|
|
indeed...
you strip the days... add 31 to the 1st...
brilliant....
(sorry, Ace_NoOne, Laszlo's *is* smarter...) _________________ Joyce Jamce |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Sun Jan 07, 2007 2:48 am Post subject: |
|
|
Well, I have already earned the smart dude title.  |
|
| Back to top |
|
 |
Joy2DWorld
Joined: 04 Dec 2006 Posts: 561 Location: Galil, Israel
|
Posted: Sun Jan 07, 2007 1:23 pm Post subject: |
|
|
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 |
|
 |
daveand5
Joined: 09 Nov 2007 Posts: 20 Location: http://taplpoker.com/
|
Posted: Wed Nov 28, 2007 7:53 am Post subject: I think I follow everything |
|
|
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 |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Nov 28, 2007 8:02 am Post subject: Re: I think I follow everything |
|
|
| 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.
 |
|
| Back to top |
|
 |
|