AutoHotkey Community

It is currently May 27th, 2012, 8:10 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: June 2nd, 2006, 12:22 pm 
Offline

Joined: October 10th, 2005, 10:44 am
Posts: 299
Location: Germany
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...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2006, 12:32 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Dear Ace_NoOne, :)

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

Regards :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2006, 1:59 pm 
Offline

Joined: October 10th, 2005, 10:44 am
Posts: 299
Location: Germany
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: )


Report this post
Top
 Profile  
Reply with quote  
PostPosted: January 7th, 2007, 12:45 am 
Offline

Joined: December 4th, 2006, 10:35 am
Posts: 561
Location: Galil, Israel
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2007, 2:01 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2007, 3:01 am 
Offline

Joined: December 4th, 2006, 10:35 am
Posts: 561
Location: Galil, Israel
indeed...




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



brilliant....



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

_________________
Joyce Jamce


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2007, 3:48 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Well, I have already earned the smart dude title. 8)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2007, 2:23 pm 
Offline

Joined: December 4th, 2006, 10:35 am
Posts: 561
Location: Galil, Israel
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


Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 28th, 2007, 8:53 am 
Offline

Joined: November 9th, 2007, 4:44 pm
Posts: 20
Location: http://taplpoker.com/
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?


Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 28th, 2007, 9:02 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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.

:)


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 13 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group