# Compute the last day of the current month

## Recommended Posts

SQLite has this:

https://www.sqlite.org/lang_datefunc.html
Compute the last day of the current month.
SQL: SELECT date('now','start of month','+1 month','-1 day');

This is my take on the calculating the end of the month in AutoIt. I simply followed the logic of the SQLite.

; Compute end of current month
Local \$sNewDate = _DateAdd('m', 1, _NowCalcDate())
MsgBox(\$MB_SYSTEMMODAL, "5", "End of this month: " & \$sNewDate)

Is this the _only_ way? Best way?

Skysnake

Why is the snake in the sky?

##### Share on other sites

Have also a look to the _DateDaysInMonth() function

here another way for example:

#include <Date.au3>

MsgBox(0, "Last day of month", LastDay())

Func LastDay(\$year = @YEAR, \$month = @MON)
Local \$LastDay = _DateDaysInMonth(\$year, \$month) ; number of days in the month
Return _DateTimeFormat(\$year & "/" & \$month & "/" & \$LastDay, 1) ; last day of month
EndFunc   ;==>LastDay

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

Without UDF, short version

; Johnmcloud - 2016
Local \$iMonth = 3, \$iYear = 2016
MsgBox(0, "Information", "Last day of month is " & _LastDayOfMonth(\$iMonth, \$iYear) & "/" & StringFormat("%02i", \$iMonth) & "/" & \$iYear)

Func _LastDayOfMonth(\$iMonth = @MON, \$iYear = @YEAR)
Switch \$iMonth
Case 2
If Mod(\$iYear, 4) = 0 And Mod(\$iYear, 100) <> 0 Or Mod(\$iYear, 400) = 0 Then Return 29
Return 28
Case 4, 6, 9, 11
Return 30
Case Else
Return 31
EndSwitch
EndFunc   ;==>_LastDayOfMonth

Edited by johnmcloud
forget calculation in leap year
##### Share on other sites

This code is plain wrong. Use _DateDaysInMonth() instead.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

What is wrong? You can't say "is wrong" without any explaination. I have calculate also the leaf year. Or do you mean the OP code? Because i haven't tried it.

Edited by johnmcloud
##### Share on other sites

Did you at least test your code?

Spoiler
Func _LastDayOfMonth_Fixed(\$iMonth = @MON, \$iYear = @YEAR)
Switch \$iMonth
Case 2
If Mod(\$iYear, 4) = 0 And (Mod(\$iYear, 100) <> 0 Or Mod(\$iYear, 400) = 0) Then Return 29
Return 28
Case 4, 6, 9, 11
Return 30
Case Else
Return 31
EndSwitch
EndFunc   ;==>LastDay

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

Yes? The example like i have write return me 31/3/2016 ( day month year ) and is correct. Other examples:

Month = 2 and Year = 2000 = 29/2/2000, correct

Month = 2 and Year = 2001 = 28/2/2001, correct

Month = 2 and Year = 2004 = 29/2/2004, correct

Other months are obviously correct... Thirty days hath September,
April, June, and November ( Case 4,6,8,9 ) all the rest have thirty-one ( Case Else )

So i'll repeat again, do you want to tell me what is "wrong" in that code?

EDIT:  I have forget "not divided by 100" for the 2100 year...next time just tell me that instead of "wrong" lol.

Edited by johnmcloud
##### Share on other sites

Because you silently changed your code in the meantime (something like 6 hours after I first made the remark in my post #4 and 2 hours after my post #6), pretending there was no error initially.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

Oh no no you totally miss the point, don't turn this around on me. Do you want to know what i pretend? The next time you see an error in my code just correct it or tell me where the error is so i can correct it. Write "the code is wrong" or "did you test it?" for me don't have any sense, is like guys that just write "don't work" without any other information, did you like it if some guy make a post like that for one of your code? I don't think because no one like to lose time. So if for you is a problem to correct me directly at first sight without make unuseful post i'm glad and i'll really appreciate it, i'm here to learn. If is a problem for you and you think your first approach at #4 are correct next time please just don't write nothing and leave the error where it is, i'm sure some other ones will correct that. Case closed, go on with the subject of the thread. Thanks and have a nice day/night,

##### Share on other sites
• Developers

Seems some one hit a nerve here....

22 minutes ago, johnmcloud said:

don't write nothing

Don't think this is what you meant right?

Jos

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

The very least when posting in Example scripts is to make reasonably certain (as far as posible) that said code is correct. Especially when said code is duplicating a function already provided in standard UDFs.

3 hours ago, johnmcloud said:

Write "the code is wrong" or "did you test it?" for me don't have any sense

This shows.

3 hours ago, johnmcloud said:

did you like it if some guy make a post like that for one of your code?

Of course yes: it forces me to look closely, debug and ... apologize flatly.

3 hours ago, johnmcloud said:

leave the error where it is

No way.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites
On 2/5/2016 at 0:08 AM, jchd said:

... apologize flatly.

I'll never apologize because i don't like this behavior which I consider absurd. If i see an error in a code i'll just write it directly.  If you want to help but not write the "solution" for a whatever reason ( i really don't understand it but seems is my problem, whatever ) i expect a positive contribute, which absolutely needs a "thanks", at least something like "hey check better the line n of your code" or "you have made a mistake in calculation of the leaf year, correct it" instead of an generic "wrong"...was too much effort, really? For me don't have any sense to make a treasure hunting when other one knows exactly where the treasure is at start, it is a useless mockery and a child play. I'm surprised of you Jos, really, that endorsing this type of behavior for the MVPs group. You're not exactly what I've expected, you really leave me disappointed. If this is the forum "guide line" I will keep it in mind and i will act accordingly next time...

##### Share on other sites

Leap Year?

Here is my take Look Ma no Mod(

local \$rM=0
local \$rY=0
For \$a= 1800 to 2400
\$rM=2
;\$rY=Random(1,3600,1)
ConsoleWrite(\$rM & "/" & \$a & " " & dom(\$rM, \$a) & " days" & @CRLF)
Next

Func dom(\$mnth,\$yr)
;here is a table of the days of the months for a reg year and a leap year
Local \$mnths, \$dom
If Lyr(\$yr) = 0 Then
Local \$mnths[13] = [0,31,28,31,30,31,30,31,31,30,31,30,31]
Else
Local \$mnths[13] = [0,31,29,31,30,31,30,31,31,30,31,30,31]
EndIf
If \$mnth > 0 and \$mnth <13  Then \$dom = Int(\$mnths[\$mnth])
Return \$dom
EndFunc

Func Lyr(\$yr)
;this just checks for a leap year same as the mod function would it takes the year/4 minus the
;integer of the year /4 and checks to see if there is a decimal if not the year divides evenly
;that makes it a leap year unless one of the next checks don't work out
;\$yr =
Local \$Lyr = 0
If (\$yr / 4 - Int(\$yr / 4)) = 0 Then \$Lyr = 1
If (\$yr / 100 - Int(\$yr / 100)) = 0 And (\$yr / 400 - Int(\$yr / 400)) <> 0 Or \$yr = 3600 Then \$Lyr = 0
;a leap year is on the years evenly divisible by four, except for century years
;unless the century year is also evenly divisible by four hundred

;3600 ad also is not a leap year, thank you Charles Kistler
Return \$Lyr
EndFunc

##### Share on other sites

Here is one with Com, using vbscript real simple

\$vbs          = ObjCreate("MSScriptControl.ScriptControl")
\$vbs.Language = "vbScript"

local \$Year  = @YEAR
local \$Month = @MON

\$lastday   = StringSplit(\$vbs.Eval("FormatDateTime(DateAdd(""d"",-1,""" & \$Year & "-" & ((\$Month + 1 = 13) ? (1) : (\$Month + 1)) & "-1""))"),"/")

\$monthname = StringSplit(\$vbs.Eval("FormatDateTime(""" & \$Year & "-" & \$Month & "-1"",1)")," ")

msgbox(0,"info",StringFormat("Year = %s\nLast Day In %s = %s",\$Year,\$monthname[2],\$lastday[2]))
##### Share on other sites

Had to change that last line to:

msgbox(0,"info",StringFormat("Year = %s\nLast Day In %s = %s",\$Year,\$monthname[2],\$lastday[1]))

lastday[2] does not seem to exist

Skysnake

Why is the snake in the sky?

##### Share on other sites

@xroot, I don't know VB, but from what I can tell, the syntax for the \$vbs.Eval is very similar to the SQLite, right?

Skysnake

Why is the snake in the sky?

##### Share on other sites

skysnake is VBS not VB. You can use whatever you like but the _DateAdd method like the one your first post is slower then the "internal" one. Check it out by yourself:

#include <Date.au3>

Local \$iYear = 2016, \$iMonth = 5
Local \$sDate, \$iBegin, \$iDiff

Sleep(1000)

\$iBegin = TimerInit()
\$sDate = _DateAdd('M', 1, \$iYear & "/" & \$iMonth & "/" & "01")
\$iDiff = TimerDiff(\$iBegin)

MsgBox(0, 0, "Timer: " & \$iDiff & @CRLF & "Last day of month is " & _DateTimeFormat(\$sDate, 2))

\$iBegin = TimerInit()
\$sDate = _LastDayOfMonth(\$iMonth, \$iYear)
\$iDiff = TimerDiff(\$iBegin)

MsgBox(0, "Information", "Timer: " & \$iDiff & @CRLF & "Last day of month is " & \$sDate & "/" & StringFormat("%02i", \$iMonth) & "/" & \$iYear)

Func _LastDayOfMonth(\$iMonth = @MON, \$iYear = @YEAR)
Switch \$iMonth
Case 2
If Mod(\$iYear, 4) = 0 And Mod(\$iYear, 100) <> 0 Or Mod(\$iYear, 400) = 0 Then Return 29
Return 28
Case 4, 6, 9, 11
Return 30
Case Else
Return 31
EndSwitch
EndFunc   ;==>_LastDayOfMonth

4.26 ms is the _DateAdd vs 0.042 ms of the Switch on my test machine, result may vary but the second one is always faster then the first. But again if you prefer _DateAdd use _DateAdd.

Edited by johnmcloud

Yes seems good

Edited by Neutro
##### Share on other sites
5 hours ago, Neutro said:

Yes seems good

... except the first one will fail in December; easy to fix tho...

## Create an account

Register a new account

• ### Similar Content

• By Skysnake
Hi
This is a request to streamline various Date and Time functions.  (PS My ability to log a ticket appears to be limited by an uncooperative server).
Let met start by saying these functions all work.  This is not an error report.
The documentation appears accurate.

My problem started here:
contains "\$sDate     Initial date in the format YYYY/MM/DD[ HH:MM:SS]"

This is in stark contrast to eg
_DateIsValid
"This function takes a date input in one of the following formats:
"yyyy/mm/dd[ hh:mm[:ss]]" or "yyyy/mm/dd[Thh:mm[:ss]]"
"yyyy-mm-dd[ hh:mm[:ss]]" or "yyyy-mm-dd[Thh:mm[:ss]]"
"yyyy.mm.dd[ hh:mm[:ss]]" or "yyyy.mm.dd[Thh:mm[:ss]]""
or
my own personal, do-anything option
_GUICtrlDTP_SetFormat
I understand that DTP and DateTime is not exactly the same.  That is not the issue.
My problem is with being able to create and TEST a valid date in at least 3 different formats, but not being able to use additional functions on such a valid date.  Assume my format is  yyyy-mm-dd based on the  "yyyy-mm-dd[ hh:mm[:ss]]" or "yyyy-mm-dd[Thh:mm[:ss]]" _DateIsValid() function.  I now have a valid function, but cannot readily use it with any of the internal DateTime functions.
Would it be possible to consider modifying the existing functions to work with the standard formats.  Put simply, if a format is accepted by _DateIsValid(), all other DateTime formats should work with same date (format) directly.
Once I have an accepted date, I want to use all internal functions on it.
Thank you for your kind attention.
Skysnake
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...