autojs时间戳怎么转换时间
在AutoJS中,可以使用Date对象将时间戳转换为日期和时间。下面是一个示例代码:
var timestamp = 1623232059696; // 时间戳,单位毫秒
var date = new Date(timestamp); // 创建一个Date对象
// 获取日期和时间
var year = date.getFullYear(); // 年份
var month = date.getMonth() + 1; // 月份(需要加1,因为月份从0开始)
var day = date.getDate(); // 日期
var hours = date.getHours(); // 小时
var minutes = date.getMinutes(); // 分钟
var seconds = date.getSeconds(); // 秒钟
// 格式化日期和时间
var formattedDate = year + '-' + padZero(month) + '-' + padZero(day); // 格式化日期
var formattedTime = padZero(hours) + ':' + padZero(minutes) + ':' + padZero(seconds); // 格式化时间
console.log('日期:' + formattedDate);
console.log('时间:' + formattedTime);
// 辅助函数:将数字补齐到两位
function padZero(num) {
return num < 10 ? '0' + num : '' + num;
}
以上代码会将时间戳转换为格式为YYYY-MM-DD HH:MM:SS的日期和时间,并在控制台输出。你可以根据需要修改格式化字符串的形式
原文地址: https://www.cveoy.top/t/topic/f832 著作权归作者所有。请勿转载和采集!