Skip to content

Instantly share code, notes, and snippets.

@gledos
Last active December 7, 2023 03:39
Show Gist options
  • Select an option

  • Save gledos/d4a94c46fd6dc92d6e63f105917df72d to your computer and use it in GitHub Desktop.

Select an option

Save gledos/d4a94c46fd6dc92d6e63f105917df72d to your computer and use it in GitHub Desktop.
Convert Date Format To ISO 8601 Style(时间转换)
// ==UserScript==
// @name Convert Date Format To ISO 8601 Style
// @namespace http://tampermonkey.net/
// @version 0.0.8
// @description Convert natural language dates to YYYY-MM-DD format
// @author ChatGPT
// @match *://*/*
// @grant GM_registerMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
// Function to convert natural language date to YYYY-MM-DD format
function convertDateFormats() {
console.log('Converting date formats...');
const elements = document.body.getElementsByTagName('*');
for (const element of elements) {
if (element.childNodes.length === 1 && element.childNodes[0].nodeType === Node.TEXT_NODE) {
const originalText = element.textContent;
// Check if the text already has a complete YYYY-MM-DD HH:MM format
if (/\d{4}-\d{2}-\d{2} \d{2}:\d{2}/.test(originalText)) {
continue; // Skip conversion if the format is already complete
}
const convertedText = originalText
// Match: 2022年6月2日 or 2022年06月02日
.replace(/(\d{4})年(\d{1,2})月(\d{1,2})日/g, (match, year, month, day) => {
// Ensure month and day have two digits
month = month.padStart(2, '0');
day = day.padStart(2, '0');
return `${year}-${month}-${day}`;
})
// Match: 2022/6/2 or 2022/06/02
.replace(/(\d{4})\/(\d{1,2})\/(\d{1,2})/g, '$1-$2-$3')
// Match: 2022-6-2 or 2022-06-02
.replace(/(\d{4})-(\d{1,2})-(\d{1,2})/g, '$1-$2-$3')
// Match: 2 June, 2022 or 02 June, 2022
.replace(/(\d{1,2}) ([a-zA-Z]+) (\d{4})/g, (match, day, month, year) => {
const monthIndex = new Date(Date.parse(`${month} 1, 2000`)).getMonth() + 1;
return `${year}-${monthIndex.toString().padStart(2, '0')}-${day.padStart(2, '0')}`;
})
// Match: June 2, 2022 or June 02, 2022
.replace(/(\d{1,2}),? ([a-zA-Z]+),? (\d{4})/g, (match, day, month, year) => {
const monthIndex = new Date(Date.parse(`${month} 1, 2000`)).getMonth() + 1;
return `${year}-${monthIndex.toString().padStart(2, '0')}-${day.padStart(2, '0')}`;
})
// Match: 2 June 2022 or 02 June 2022
.replace(/([a-zA-Z]+) (\d{1,2}), (\d{4})/g, (match, month, day, year) => {
const monthIndex = new Date(Date.parse(`${month} 1, 2000`)).getMonth() + 1;
return `${year}-${monthIndex.toString().padStart(2, '0')}-${day.padStart(2, '0')}`;
})
// Match: 22-6-2 12:18 or 22-06-02 12:18
.replace(/(\d{2})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2})/g, (match, year, month, day, hour, minute) => {
// Ensure month, day, hour, and minute have two digits
month = month.padStart(2, '0');
day = day.padStart(2, '0');
hour = hour.padStart(2, '0');
minute = minute.padStart(2, '0');
return `20${year}-${month}-${day} ${hour}:${minute}`;
})
// Match: 2022-6-2
.replace(/(\d{4})-(\d{1,2})-(\d{1,2})/g, (match, year, month, day) => {
// Ensure month and day have two digits
month = month.padStart(2, '0');
day = day.padStart(2, '0');
return `${year}-${month}-${day}`;
});
if (convertedText !== originalText) {
console.log('Converted: ', originalText, ' => ', convertedText);
element.textContent = convertedText;
element.setAttribute('data-original-text', originalText);
}
}
}
}
// Register menu command to convert date formats
GM_registerMenuCommand('Convert Date Formats', convertDateFormats);
// Listen for Alt+C key press to convert date formats
window.addEventListener('keydown', (e) => {
if (e.altKey && e.key === 'c') {
convertDateFormats();
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment