Date Tag From Title


name

Date Tag From Title

Icon

🗓️

Setting

Parent tag

Setting

Revert to today upon failure

Description

Tag current note with date from note title.

Tag will be formatted as yyyy/mm/dd.

Correctly parses Amplenote's Month NNst, YYYY format.

If it fails to parse a date from the note title, it can revert to the current date, if you have set it to do so in the options.

Though browser dependent and may vary, sample working date formats should include:

2023-12-22

December 22nd, 2023

Dec 22nd, 2023

Dec 22 2023

2023DEC22

2023-DEC-15

12-05-2023 (Interprets as: December 05, 2023)


{
async noteOption(app, noteUUID) {
const note = await app.notes.find(noteUUID);
const title = note.name;
var revert_to_today_upon_failure;
var d;
var y;
var m;
var day;
 
const parts = title.trim().split(" ");
/*----====----====----====----====
First attempt: Look for Amplenote date format from string start position.
----====----====----====----====*/
if(parts.length > 2){
if(title.match(/(January|February|March|April|May|June|July|August|September|October|November|December)\s(\d{1,2})(st|nd|rd|th),\s(\d{4})/i)){
var match = title.match(/(January|February|March|April|May|June|July|August|September|October|November|December)\s(\d{1,2})(st|nd|rd|th),\s(\d{4})/i);
console.log(match);
match = match[0].replace("1st","1").replace("2nd","2").replace("3rd","3").replace("4th","4").replace("5th","5").replace("6th","6").replace("7th","7").replace("8th","8").replace("9th","9").replace("0th","0");
d = new Date(match);
}else if(title.match(/(Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sept|September|Oct|October|Nov|November|Dec|December)\s(\d{1,2}),\s(\d{4})/i)){
var match = title.match(/(Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sept|September|Oct|October|Nov|November|Dec|December)\s(\d{1,2}),\s(\d{4})/i);
d = new Date(match[0]);
}
}
 
 
/*----====----====----====----====
If fails, next attempt: Look through entire title for dates.
----====----====----====----====*/
if(isNaN(d)){
for(var i = 0; i < parts.length; i++){
d = new Date(parts[i]);
/* if successful then break out of the loop */
if(!isNaN(d)){break;}
}
}
 
/*----====----====----====----====
If still fails, revert to today if user accepted that option
----====----====----====----====*/
try{
revert_to_today_upon_failure = app.settings['Revert to today upon failure'];
if(revert_to_today_upon_failure == 'y' || revert_to_today_upon_failure == 'true' || revert_to_today_upon_failure == '1'){
revert_to_today_upon_failure = 'yes';
}
}catch(e){
revert_to_today_upon_failure = 'no';
}
if(isNaN(d) && revert_to_today_upon_failure == 'yes'){
d = new Date();
}
 
/*----====----====----====----====
Final output
----====----====----====----====*/
if(!isNaN(d)){
/* account for user's time zone */
d.setMinutes(d.getMinutes() + d.getTimezoneOffset());
y = d.getFullYear();
m = ('0' + (d.getMonth() + 1)).slice(-2);
day = ('0' + d.getDate()).slice(-2);
var parent_tag = await (app.settings["Parent tag"]) || "";
var final_tag = parent_tag + y + "/" + m + "/" + day;
const added = await note.addTag(final_tag);
}
}
}