Last day of current month

Started by mjwillyone, August 16, 2021, 04:06:57 PM

Previous topic - Next topic

mjwillyone

Hello, I am trying to get the last day of the current month, even if I am at the 15th of the month when running the script.  Can anyone help?

Thank you!
Mike

td

An almost but not quite non-mathematical solution is to create a list of the length of each month by using the  MM from the YYYY:MM:DD:HH:MM:SS returned by TimeYmdHms as the index into the list.

The math part comes in when you need to determine if the current year is a leap year.  Here is a link to a Tech Db example script exactly for that purpose:

https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/UDF~-~UDS~Library/Time~UDFs+Determine~Leap~Year.txt
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

mjwillyone

Thank you very much!  I will give this a "whirl!"

Mike

ChuckC

One technique I have used to get the desired result is as follows:

Call TimeYmdHms() to get the current date & time.

Parse out the year and month portions, then either increment the month by one, or, in the case of December, set the month to one and increment the year, and set the day of month to one.  Build a new YmdHms string with the new year, month & day values, leaving the "Hms" portion as zeros.

Call TimeSubtract() to subtract one day from the newly calculated date & time string.

The result is a YmdHms formatted string containing the date for the last date of the current month.


kdmoyers

Everyone has their favorite way to do this.  Here's mine:
Code (winbatch) Select
t1 = timeymdhms()                   ; today
t2 = timeadd(t1, "0:1:0:0:0:0")     ; plus one month
t3 = strsub(t2,1,7):":1:0:0:0"      ; the 1st of that month
t4 = timesubtract(t3,"0:0:1:0:0:0") ; minus one day
message('',t1:@lf:t2:@lf:t3:@lf:t4) ; last day of this month

((which I see is same as what ChuckC said))
The mind is everything; What you think, you become.

td

Good approach. Less "Rude Goldbergian" than my suggestion.

[edit] TimeAdd and TimeSubtract do automagically adjust the year when the respective operation roles to or back from a given year. That makes the approach even cleaner.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

Probably not as efficient as Kirby's but for what it is worth...

Code (winbatch) Select

last_day = ItemReplace("28",3,TimeYMDHMS(),":")
While TimeFormat(TimeAdd(last_day, "0:0:1:0:0:0"),"d") != 1
  last_day = TimeAdd(last_day, "0:0:1:0:0:0")
EndWhile

Message('',last_day)


Jim

stanl

Allow me to complicate things... if user has Office installed there is a worksheet function that is pretty versatile, per the second parameter:
Code (WINBATCH) Select


;assume Microsoft Office Installed
;something to play around with


gosub udfs
base = "1899:12:31:00:00:0"
Decimals(0)


oXL = CreateObject("Excel.Application")
oXL.Visible          = @FALSE  ; change this to @FALSE to run hidden
oXL.ScreenUpdating   = @TRUE  ; if running hidden, change this to @FALSE
oXL.UserControl      = @TRUE
oXL.DisplayAlerts    = @FALSE
d = TimeFormat(TimeYmdHms(),"MM/dd/yyyy")  ;set for time
diff = oXL.WorksheetFunction.EoMonth(d,0)-1
mdates("End of Current Month")
diff = oXL.WorksheetFunction.EoMonth(d,-1)
mdates("Begin Current Month")
diff = oXL.WorksheetFunction.EoMonth(d,0)
mdates("Start of Next Month")
diff = oXL.WorksheetFunction.EoMonth(d,-1)-1
mdates("End Previous Month")
oXL.Quit()
oXL=0


:udfs
#DefineSubRoutine mdates(var)
AddTime= "0000:00:":diff:":00:00:0"
Message("Current Date: ":d,var:@LF:TimeFormat(TimeAdd(base, AddTime),"MM/dd/yyyy"))
Return(1)
#EndSubRoutine


Return





mjwillyone

I so much appreciate this help!  I was able to make it work and I am thrilled! 

I love this community.  So much help.  Oh I wish I were at a stage to offer my help to others .. but I assume that will come!

Thank you again!!
Mike