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.
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:
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"
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.
The last day of this month
using the ICUDateTime approach?
I can get what I want by putting "31" in place of the "1", and it works for December, but I have a feeling that won't work for months that have only 30 days - i.e.using "31" in February will generate March 3rd, not the end of February.