#popclip
name: Time Converter
icon: symbol:arrow.triangle.2.circlepath
requirements: [popup]
javascript: |
// 检查是否为10位数字时间戳
const isTimestamp = /^\d{10}$/.test(popclip.input.text);
// 检查是否为YYYY-MM-DD HH:MM:SS格式
const dateRegex = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
const isDateTime = dateRegex.test(popclip.input.text);
if (isTimestamp) {
// 时间戳转日期时间
// 将时间戳转换为毫秒
const timestamp = parseInt(popclip.input.text) * 1000;
// 创建Date对象
const date = new Date(timestamp);
// 转换为东八区(UTC+8)时间
const utc8Date = new Date(date.getTime() + 8 * 60 * 60 * 1000);
// 格式化为YYYY-MM-DD HH:MM:SS
const formattedDate = utc8Date.toISOString().replace('T', ' ').slice(0, 19);
// 在浮窗中显示转换后的时间
popclip.showText(formattedDate);
} else if (isDateTime) {
// 日期时间转时间戳
const match = popclip.input.text.match(dateRegex);
// 解析日期时间字符串
const year = parseInt(match[1]);
const month = parseInt(match[2]) - 1; // JavaScript的月份是0-11
const day = parseInt(match[3]);
const hour = parseInt(match[4]);
const minute = parseInt(match[5]);
const second = parseInt(match[6]);
// 使用UTC方法创建Date对象,确保时区处理正确
const date = new Date(Date.UTC(year, month, day, hour, minute, second));
// 从UTC+8调整到UTC(减去8小时)
const utcTime = date.getTime() - 8 * 60 * 60 * 1000;
// 转换为10位时间戳
const timestamp = Math.floor(utcTime / 1000);
// 在浮窗中显示时间戳
popclip.showText(timestamp.toString());
} else {
// 如果不是上述两种格式,则不显示
return null;
}