Jump to content

Compute the last day of the current month


Skysnake
 Share

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())
$sNewDate = _DateAdd('d', -1, $sNewDate)
MsgBox($MB_SYSTEMMODAL, "5", "End of this month: " & $sNewDate)

Is this the _only_ way? Best way? 

Skysnake

Why is the snake in the sky?

Link to comment
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

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
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
Link to comment
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.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
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)

Link to comment
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.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
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)

Link to comment
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
Link to comment
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.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
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)

Link to comment
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,

Link to comment
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
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)

Link to comment
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...

Link to comment
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

 

Link to comment
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]))
Link to comment
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")
$sDate = _DateAdd('D', -1, $sDate)
$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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...