How to Get Date of the Last Day of Previous Month

help

#1

I've looked around the forum and search Google for Applescript & Javascript date calculations but can't figure it out. I need to get the last day (mm-dd-yy) of the previous month and none of the examples I've found work, I get long string of numbers. I got rid of the code I was trying before coming here, I should have kept it so I could share what I've tried so far - so I apologize.

Can anyone help me figure this out? It seems X-Paths aren't working to download my bank statements and if I can automatically grab the last month's date, I could automate the file downloads.


#2

You can do this in Javascript:

const lastDayOfPreviousMonth = (curMonth) => {
  curMonth.setDate( 1 )
  curMonth.setHours( -1 )
  return curMonth
}

Sample output:

console.log( lastDayOfPreviousMonth( new Date() ) )
//Fri Nov 30 2018 23:51:57 GMT-0800 (Pacific Standard Time)

console.log( lastDayOfPreviousMonth( new Date('2018-10-16') ) )
//Sun Sep 30 2018 23:00:00 GMT-0700 (Pacific Daylight Time)

console.log( lastDayOfPreviousMonth( new Date('2018-07-22') ) )
//Sat Jun 30 2018 23:00:00 GMT-0700 (Pacific Daylight Time)

console.log( lastDayOfPreviousMonth( new Date('2018-01-08') ) )
//Sun Dec 31 2017 23:00:00 GMT-0800 (Pacific Standard Time)

I leave formatting the date to the desired pattern as an exercise for the user.


#3

Use the ICUDateTimeFor token.

%ICUDateTimeFor%TIME(YEAR(),MONTH(),1,12,0,0)-86400%MM-dd-yy%

Basically, get the first day of this month (midday on the 1st GMT, which is pretty much guaranteed to be within the first day of the month local time), and then subtract a days worth of seconds (86400).

You could also use -12 for the hours, or 0 for the day:

%ICUDateTimeFor%TIME(YEAR(),MONTH(),1,-12,0,0)%MM-dd-yy%
%ICUDateTimeFor%TIME(YEAR(),MONTH(),0,12,0,0)%MM-dd-yy%

#4

Along with the two very good methods already demonstrated here, I offer the AppleScript method:

tell (the current date) to set lastdayoflastmonth to it - (its day) * days

This will yield a full AppleScript date object of the form date "Friday, 30 November 2018 at 11:59:06", but being an AppleScript date object, you can extract some useful properties from it to suit your needs:

Day of the month: day of lastdayoflastmonth --> 30
Day of the week: weekday of lastdayoflastmonth --> Friday
Full date only: date string of lastdayoflastmonth --> "Friday, 30 November 2018"


#5

Use a shell script to set a variable and then use as you desire?

date -v1d -v-1d

…will get you the final day of the previous month you are in.

Fri Nov 30 17:14:17 EST 2018


date -v3m -v1d -v-1d

…will get you the final day of the month before the THIRD month (-v3m)

Wed Feb 28 17:16:36 EST 2018


Breaking it down, just tell date the date you wish to work on. By default, it's today. So, in the first command, you're telling date to go to the 1st day (-v1d) of this month (default is always today, hence always this month, year, moment, etc.) and the the final parameter tells date to subtract one day, garnering the final day of the month before.

In the second command you're telling date to go to the 3rd month (-v3m), 1st day (-v1d) and subtract one day (-v-1d) resulting in Feb 28.

You can format the output any way you wish with standard unix date formatting, and either use the result directly or set a variable to use as you wish. HTH.

In your case, to achieve mm-dd-yy output your date command would be:

date -v1d -v-1d '+%m-%d-%y'

which would yield…

11-30-18 considering the date of this writing is Sat, Dec 8, 2018.