Jump to content


Photo

Total number of the current month


  • Please log in to reply
17 replies to this topic

#1 keekehhung

keekehhung
  • Members
  • 4 posts

Posted 28 March 2006 - 07:59 AM

Greetings,

May I ask, do anybody know how to calculate the total number of current month? or the last day of current month? For example,

1) MARCH 2006 = 31 days.
2) APRIL 2006 = 30 days

Thank you
Kee

#2 BoBo

BoBo
  • Guests

Posted 28 March 2006 - 08:44 AM

FormatTime
--------------------------------------------------------------------------------
Transforms a YYYYMMDDHH24MISS timestamp into the specified date/time format.

FormatTime, OutputVar [, YYYYMMDDHH24MISS, Format]

YYYYMMDD... - Leave this parameter blank to use the current local date and time. Otherwise, specify all or the leading part of a timestamp in the YYYYMMDDHH24MISS format. If the date and/or time portion of the timestamp is invalid -- such as February 29th of a non-leap year -- the date and/or time will be omitted from OutputVar. Although only years between 1601 and 9999 are supported, a formatted time can still be produced for earlier years as long as the time portion is valid.


FormatTime, OutPutVar, 20060331, ShortDate ; valid date
MsgBox % OutPutVar
FormatTime, OutPutVar, 20060332, ShortDate ; invalid date - no output
MsgBox % OutPutVar


#3 keekehhung

keekehhung
  • Members
  • 4 posts

Posted 28 March 2006 - 09:01 AM

Hi, Bobo.

Thanks for your help. I just managed to find a post from PenP. after some amendment on the code, now the coding can be calculate the length of the current month.

Get the solution from following thread. :)
http://www.autohotke... ... ight=month


yday = %a_mday% 
ymonth = %a_mon% 
yyear = %a_year% 

  if ymonth = 01 
  { 
    yday = 31 
  } 
  else if ymonth = 02 
  { yday = 28 
    leapyear = %a_year% 
    envdiv, leapyear, 4 
    envmult, leapyear, 4 
    envsub, leapyear, %a_year% 
  ifequal leapyear, 0 
  { 
    yday = 29 
  } 
  } 
  else if ymonth = 03 
  { 
    yday = 31 
  } 
  else if ymonth = 04 
  { 
    yday = 30 
  } 
  else if ymonth = 05 
  { 
    yday = 31 
  } 
  else if ymonth = 06 
  { 
    yday = 30 
  } 
  else if ymonth = 07 
  { 
    yday = 31 
  } 
  else if ymonth = 08 
  { 
    yday = 31 
  } 
  else if ymonth = 09 
  { 
    yday = 30 
  } 
  else if ymonth = 10 
  { 
    yday = 31 
  } 
  else if ymonth = 11 
  { 
    yday = 30 
  } 
  else if ymonth = 12 
  { 
    yday = 31 
  } 
;} 


MsgBox, This month have: `t%yday% days.


#4 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 28 March 2006 - 09:26 AM

Dear keekehhung, :)

I've just posted LDOM() {returns Last Day of Month}

Give it a try...

Regards :)

Edit:

MsgBox, % "This month have: " LDOM() " days."


#5 BoBo

BoBo
  • Guests

Posted 28 March 2006 - 10:47 AM

Slight variation to Goyyah's LDOM() function
TimeStamp= 20060229

Status := BoBoLDOM(TimeStamp)

MsgBox % TimeStamp "=" Status ; caller section  for testing purpose



BoBoLDOM(TimeStamp)

{

FormatTime, DayNum,% TimeStamp,dd

FormatTime, Month,% TimeStamp,MM

If InStr(DayNum,31) OR InStr(DayNum,30) InStr(DayNum,29) OR InStr(DayNum,28)

   Return ("VALID") ; MsgBox Day %DayNum% is a valid day in %Month%

Else

	Return ("INVALID") ; MsgBox Day %DayNum% isn't a valid day in %Month%

  }
Tested. 8)

#6 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 28 March 2006 - 11:22 AM

Variant of BoBoLDOM() 8)

TimeStr=20000229
ValidDate := IsDateValid(TimeStr)

If ValidDate
   Msgbox % TimeStr " is a Valid Date"
else
   Msgbox % TimeStr " is an Invalid Date!"
return

IsDateValid(TimeStr)
{
FormatTime,Dt,%TimeStr%,d
If Dt = 
   Return 0
else
   Return 1
}

Regards, :)

#7 BoBo

BoBo
  • Guests

Posted 28 March 2006 - 11:36 AM

Interesting, right after I've posted that "draft" I've shrinked it to something which is quite similar to yours :lol:

Thanks. 8)

#8 keekehhung

keekehhung
  • Members
  • 4 posts

Posted 29 March 2006 - 12:00 AM

WOW!! You guys really great! Thanks a lot :p

#9 Laszlo

Laszlo
  • Fellows
  • 4713 posts

Posted 29 March 2006 - 02:09 AM

You could still chop off a couple of lines
IsDateValid(TimeStr) { 

   FormatTime,Dt,%TimeStr%,d 

   Return DT > ""

}
If you like it more cryptic, use
Return 1-!DT


#10 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 29 March 2006 - 06:27 AM

Dear Laszlo, :)

Thanks a lot.

If you like it more cryptic, use

Return 1-!DT


:shock:

Can you please explain/give me one more example on this. I've never used anything like it!

Please...

Regards, :)

#11 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 29 March 2006 - 07:33 AM

Let me guess...
If valid, DT is a number between 1 and 31, which is true; !DT transform it to false, ie. 0 when used in a numeric expression.
If invalid, DT is empty, but !DT transforms it from implicit false to true, ie. 1 when used in a numeric expression.
So, 1 - !DT is 0 (false) if DT is invalid, and 1 (true) if valid.
Very smart and tricky... :-)
We can also use Return not !DT (!!DT doesn't work...) or simply Return DT > 0.

#12 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 29 March 2006 - 08:15 AM

If valid, DT is a number between 1 and 31, which is true; !DT transform it to false, ie. 0 when used in a numeric expression.
If invalid, DT is empty, but !DT transforms it from implicit false to true, ie. 1 when used in a numeric expression.
So, 1 - !DT is 0 (false) if DT is invalid, and 1 (true) if valid.


Nicely explained.. Thanks a lot PhiLho.. :D

One more thing. I understand Zero is False and any other number is True

But why does this not work? : IfEqual,DT,,Return 0

Regards, :)

#13 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 29 March 2006 - 10:45 AM

But why does this not work? : IfEqual,DT,,Return 0

Works for me...
IsDateValid(_timeStr) {

	FormatTime DT, %_timeStr%, d

	IfEqual DT, , Return 0

	Return 1

}

MsgBox % IsDateValid("20060330")  IsDateValid("20060230")


#14 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 29 March 2006 - 11:05 AM

Oh Yes!.. It does work!!, Guess I was blind !!! :shock:.

Thanks PhiLho. Regards, :)

#15 Laszlo

Laszlo
  • Fellows
  • 4713 posts

Posted 29 March 2006 - 04:38 PM

"1 - !DT" is in the direction of obfuscating the code (to enforce copyrights). PhiLho just blew the cover... :lol:

Chris: should not "!!x" work? Or "not not x"?
"not !x", "!(! x)" and "not (not x)" do work, but "! not x" does not.