Author Topic: Last day of current month  (Read 277 times)

mjwillyone

  • Newbie
  • *
  • Posts: 49
Last day of current month
« on: August 16, 2021, 04:06:57 pm »
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

  • Tech Support
  • *****
  • Posts: 3947
    • WinBatch
Re: Last day of current month
« Reply #1 on: August 16, 2021, 04:52:43 pm »
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

  • Newbie
  • *
  • Posts: 49
Re: Last day of current month
« Reply #2 on: August 16, 2021, 04:57:45 pm »
Thank you very much!  I will give this a "whirl!"

Mike

ChuckC

  • Sr. Member
  • ****
  • Posts: 333
Re: Last day of current month
« Reply #3 on: August 17, 2021, 04:44:06 am »
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

  • Sr. Member
  • ****
  • Posts: 434
Re: Last day of current month
« Reply #4 on: August 17, 2021, 04:47:40 am »
Everyone has their favorite way to do this.  Here's mine:
Code: Winbatch
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

  • Tech Support
  • *****
  • Posts: 3947
    • WinBatch
Re: Last day of current month
« Reply #5 on: August 17, 2021, 06:19:12 am »
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

  • Pundit
  • *****
  • Posts: 1743
    • Data & Stuff Inc.
Re: Last day of current month
« Reply #6 on: August 17, 2021, 07:56:58 am »
Probably not as efficient as Kirby's but for what it is worth...

Code: Winbatch
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

  • Pundit
  • *****
  • Posts: 1572
Re: Last day of current month
« Reply #7 on: August 17, 2021, 11:15:55 am »
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

;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

  • Newbie
  • *
  • Posts: 49
Re: Last day of current month
« Reply #8 on: August 20, 2021, 05:10:57 am »
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