# Rename with last day of month when source only contains year-month

**URL:** <https://discourse.devontechnologies.com/t/rename-with-last-day-of-month-when-source-only-contains-year-month/68175>\
**Category:** Automation\
**Tags:** devonthink3, automation\
**Created:** [December 5, 2021, 8:06pm UTC](https://discourse.devontechnologies.com/t/rename-with-last-day-of-month-when-source-only-contains-year-month/68175 "2021-12-05T20:06:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fcy](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/fcy/32/23267_2.png) [@fcy](https://discourse.devontechnologies.com/u/fcy)\
**Post date:** [December 5, 2021, 8:06pm UTC](https://discourse.devontechnologies.com/t/rename-with-last-day-of-month-when-source-only-contains-year-month/68175/1 "2021-12-05T20:06:36Z")

</div>

Hello there,

I’m creating smart rules to automatically rename and file my monthly statements and most of them have the complete date information, but there is one where I only have the month and year, and I’d like to rename it to has the last day of that month.

My input is a file named `Brokerage Statement - XXXX1234 - 202111` where the last digits are the date with the format `yyyymm`, I create the following Smart Rule to detect the date but it gives me the first day of the month.

 ![Screen Shot 2021-12-05 at 12.03.55](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/9/c/9c0d62e298312d8921d17336a283f17fd45a42a4.png)

Is there a way to convert it to the last day of the month before I rename it?

I tried using the `Oldest Document Date` placeholder but it gives me a totally unexpected result: with input = 202111, results in 2021-01-20.

---

<div class="post-metadata">

**Author:** ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)\
**Post date:** [December 5, 2021, 8:33pm UTC](https://discourse.devontechnologies.com/t/rename-with-last-day-of-month-when-source-only-contains-year-month/68175/2 "2021-12-05T20:33:08Z")

</div>

My goto is Applescript

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [December 5, 2021, 8:48pm UTC](https://discourse.devontechnologies.com/t/rename-with-last-day-of-month-when-source-only-contains-year-month/68175/3 "2021-12-05T20:48:15Z")

</div>

Or this other scripting language. I’d use the first day of the _next_ month and then subtract one day. That should work with leap years too, but one has to take care of December/January.

---

<div class="post-metadata">

**Author:** ![fcy](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/fcy/32/23267_2.png) [@fcy](https://discourse.devontechnologies.com/u/fcy)\
**Post date:** [December 5, 2021, 10:25pm UTC](https://discourse.devontechnologies.com/t/rename-with-last-day-of-month-when-source-only-contains-year-month/68175/4 "2021-12-05T22:25:17Z")

</div>

I thought I’d have to go the scripting route, is there a way to pass parameters to the script? In this I’d like to send the script the regex groups that Scan Name found. The actions would be something like this:

1. Scan Name, Date
2. Execute Script to rename the file - with the output of scan name as input for the script
3. Move

---

<div class="post-metadata">

**Author:** ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)\
**Post date:** [December 6, 2021, 12:11am UTC](https://discourse.devontechnologies.com/t/rename-with-last-day-of-month-when-source-only-contains-year-month/68175/5 "2021-12-06T00:11:30Z")

</div>

The record is passed to the script  
See the skeleton script below

```auto
on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
                                           <<< insert code
		end repeat
	end tell
end performSmartRule

```

> I’d like to rename it to has the last day of that month

Here’s example code to get the last day of the month and rename the record

```auto
			set theName to name of theRecord
			set theName to theName & my LastDay(texts -6 thru -1 of theName)
			set name of theRecord to theName

on LastDay(theDate)
	set theyyyy to characters 1 thru 4 of theDate as string
	set themm to characters 5 thru 6 of theDate as string
	-- Convert to the first day of the following month
	-- Then get the previous day
	set NewDate to current date
	set year of NewDate to theyyyy
	set day of NewDate to 1
	set month of NewDate to (themm as integer) + 1
	set NewDate to NewDate - (1 * days)
	return day of NewDate
end LastDay

```

---

<div class="post-metadata">

**Author:** ![fcy](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/fcy/32/23267_2.png) [@fcy](https://discourse.devontechnologies.com/u/fcy)\
**Post date:** [December 6, 2021, 6:31am UTC](https://discourse.devontechnologies.com/t/rename-with-last-day-of-month-when-source-only-contains-year-month/68175/6 "2021-12-06T06:31:29Z")

</div>

Thank you! While my brain finds AppleScript only workable when I have it fully ready to use and only need a few tweaks your example made me try to do it in JavaScript and I got there:

```auto
function performsmartrule(records) {
	records.forEach (record => {
		const originalName = record.nameWithoutExtension()
		const rawDate = originalName.match(/test.+(\d{4})(\d{2})/)
		const year = rawDate[1]
		const month = rawDate[2]
		// JS month is zero-indexed, to get last day of a month we pass 0 of the next month
		const lastDay = new Date(year, month, 0).getDate()
		const documentDate = [year, month, lastDay].join("-")
		
		const newName = record.name().replace(originalName, documentDate + " - Test")
		record.name = newName
	})
}

```

It even works for leap year. Given the file `test something 202002.txt` it gets renamed to `2020-02-29 - Test.txt`.

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [December 6, 2021, 7:45am UTC](https://discourse.devontechnologies.com/t/rename-with-last-day-of-month-when-source-only-contains-year-month/68175/7 "2021-12-06T07:45:01Z")

</div>

> [@fcy](#):
>
> `const lastDay = new Date(year, month, 0).getDate()`

I’ve never seen that before. 👍
