Created
June 17, 2024 08:55
-
-
Save pilotmoon/0097f1de5d9a37fbd5652f4fc100efce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #popclip | |
// name: Reformat Date | |
// icon: symbol:calendar | |
// description: Convert date to either DD-MMM-YYYY or YYYY-MM-DD. | |
// options: | |
// - { identifier: style-mmm, label: "DD-MMM-YYYY", type: boolean, icon: "monospaced square filled MMM" } | |
// - { identifier: style-iso, label: "YYYY-MM-DD", type: boolean, icon: "monospaced square filled ISO" } | |
// language: javascript | |
// module: true | |
// | |
// Define the output format options | |
const DateFormat1 = { day: "2-digit", month: "long", year: "numeric" }; // "29 January 2024" | |
const DateFormat2 = { day: "2-digit", month: "short", year: "numeric" }; // "29 Jan 2024" | |
const DateFormat3 = { day: "2-digit", month: "2-digit", year: "numeric" }; // "29/01/2024" | |
// | |
var strTemp; | |
// | |
function convertDateFormat(inputText, dateOption) { | |
// function to convert format of date passed as inputText | |
// | |
// If inputText is all whitespace then use today's date instead | |
const inputDate = | |
inputText.trim().length > 0 ? new Date(inputText.trim()) : new Date(); | |
// Check if the inputDate is valid | |
if (isNaN(inputDate)) { | |
// Date is not valid, return the input text | |
return "'" + inputText + "' is not a valid date format"; | |
} else { | |
switch (dateOption) { | |
case DateFormat1: | |
// 29 January 2024 | |
return Intl.DateTimeFormat("en-GB", DateFormat1).format(inputDate); // "29 January 2024" | |
break; | |
case DateFormat2: | |
// 29-Jan-2024 | |
return Intl.DateTimeFormat("en-GB", DateFormat2) | |
.format(inputDate) | |
.replace(/\s+/g, "-"); // "29-Jan-2024" | |
break; | |
case DateFormat3: | |
// 2024-01-29 | |
strTemp = Intl.DateTimeFormat("en-GB", DateFormat3).format(inputDate); // "29/01/2024" | |
strTemp = strTemp.split("/"); | |
return strTemp[2] + "-" + strTemp[1] + "-" + strTemp[0]; // "2024-01-29" | |
break; | |
} | |
} | |
} | |
// An action for each format | |
exports.actions = [ | |
{ | |
title: "DD-MMM-YYYY", | |
icon: "monospaced square filled MMM", | |
requirements: ["option-style-mmm=1"], | |
code: () => { | |
popclip.pasteText(convertDateFormat(popclip.input.text, DateFormat2)); // "29-Jan-2024" | |
}, | |
}, | |
{ | |
title: "YYYY-MM-DD", | |
icon: "monospaced square filled ISO", | |
requirements: ["option-style-iso=1"], | |
code: () => { | |
popclip.pasteText(convertDateFormat(popclip.input.text, DateFormat3)); | |
("2024-01-29"); | |
}, | |
}, | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment