Template to remove date from note title; detects YYYYMMDD and YYYY-MM-DD

I had a bunch of old notes titled with the creation date. Example: “20190510 ODBC connection to Oracle DB from R”, or “2021-01-04 Demystifying Artificial Intelligence”.

Soon, with all the packages available in Obsidian, I found titling the notes with a date was not practical, and looked a bit ugly to me. So, I started to rename my notes. In need of some automation. I wrote this script with some help from reading the posts here in the forums, and some JavaScript in StackOverflow.

Here it is:

<%*
/*  Script that removes a date stamped at the beginning of the note.
	The date could be either in the YYYYMMDD or YYYY-MM-DD format.
*/
let title = tp.file.title   // title of the note
let originalTitle = title   // store it in summary
let xD = ""
let date = ""
var result = ""
// next two line check for the presence of a date
let isDashedDate = (/^\d+-\d+-\d+/).test(title)    // returns boolean
let isUndashedDate = (/^\d+\d+\d+/).test(title)    // returns boolean
let restOfChars = ""
// now, we check dates, remove them and form new note title
if (!isDashedDate) {
	// note could contain a date but is not of the dashed form
	if (isUndashedDate) {
		// the note does contain a YYYYMMDD date at the beginning
		restOfChars = title.substr(9, title.length);  // capture text after date
		xD = title.substr(0, 8);                      // capture date
		date = tp.date.now("YYYY-MM-DD", 0, xD, "YYYYMMDD")  // to YYYY-MM-DD
		// convert 1st character of title to uppercase, join the rest of text
		result = restOfChars.charAt(0).toUpperCase(0) + restOfChars.slice(1)
	} else {
		result = title;
	}
} else {
	xD = title.substr(0, 10);
	date = tp.date.now("YYYY-MM-DD", 0, xD, "YYYY-MM-DD") 
	restOfChars = title.substr(11, title.length);
	result = restOfChars.charAt(0).toUpperCase(0) + restOfChars.slice(1)
}
title = result
await tp.file.rename(title);    // rename the note
-%>
<%_* /* Now comes the YAML properties */ -%>
---
aliases: 
type:
title: <%* tR += title %>
tags: #renamed   <%_* /*  adding a tag doesn't hurt   */ %>
date: <% tp.date.now("YYYY-MM-DD") %>
dow: <% tp.date.now("dddd") %>
created: <%* tR += date; %>
cdow: <% tp.date.now("dddd", 0, date, "YYYY-MM-DD") %>
<%_* /*    We save the original title for a future case           */ %>
summary: "Original title was: <%* tR += originalTitle; %>"
up:
related: []
rating: 
uuid: <% tp.date.now("YYYYMMDDhhmmss", 0, date, "YYYYMMDD") %>
---
<%* await tp.file.move("/Renamed/" + title) %>

As you can see, I added a bunch a comments, which are invisible at the moment that the template get executed on the note.