Custom Date/Time Stamp Plugin

name

date

icon

today

description

Inserts a date stamp using the date format and am/pm settings

instructions

Configure the format variable. The following letters can be used for various parts of the date. Any other letter will be left untouched.

y = four digit year

m = two digit month

d = two digit date

h = two digit hours (AM/PM is controlled via a separate variable)

n = two digit minutes

s = two digit seconds

Set the am/pm variable to true for 12 hour time or false for military time.

Anywhere in a note, type {date} to have it replaced with your custom date stamp.



As another example, setting the format to y-m-d [h:n:s] returns: 2023-04-22 [11:04:43 PM]

setting

date format

setting

am/pm (set true to return 12 hour, false to return military time)


{
insertText(app) {
function countChar(str, char) {
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === char) {
count++;
}
}
return count;
}
 
// This function changes 'y's, 'm's, and 'd's into the current year,
// month, or date respectively. Any other character is left unchanged.
function formatDate(dateString, dateObj, ampm=true) {
const year = dateObj.getFullYear().toString();
const month = (dateObj.getMonth() + 1).toString();
const day = dateObj.getDate().toString();
const hours = dateObj.getHours().toString().padStart(2, '0');
const minutes = dateObj.getMinutes().toString().padStart(2, '0');
const seconds = dateObj.getSeconds().toString().padStart(2, '0');
let addAMPM = false;
let formattedString = '';
 
for (let i = 0; i < dateString.length; i++) {
switch (dateString[i]) {
case 'y':
formattedString += year;
break;
case 'm':
formattedString += month.padStart(2, '0');
break;
case 'd':
formattedString += day.padStart(2, '0');
break;
case 'h':
if (ampm) {
addAMPM = true;
const hour12 = hours % 12 || 12;
formattedString += hour12.toString();
} else {
formattedString += hours.toString().padStart(2, '0');
}
break;
case 'n':
formattedString += minutes;
break;
case 's':
formattedString += seconds;
break;
default:
if (addAMPM && dateString[i] != ":") {
addAMPM = false
const amPm = hours < 12 ? ' AM' : ' PM';
formattedString += amPm;
}
formattedString += dateString[i];
break;
}
}
 
if (addAMPM) {
addAMPM = false;
const amPm = hours < 12 ? ' AM' : ' PM';
formattedString += amPm;
}
return formattedString;
}
 
// Get settings from the main application and create the time stamp
let formatString = String(app.settings["date format"]);
let ampm = String(app.settings["am/pm (set true to return 12 hour, false to return 24 military time)"]);
let today = new Date();
let timeStamp = formatDate(formatString, today, ampm);
return timeStamp;
}
}