Skip to content

js时间戳

时间 转 时间戳

第一种

js
var timestamp = Date.now();
var timestampInSeconds = timestamp / 1000;
console.log(timestampInSeconds)   //1670557624.589
Math.floor(timestampInSeconds)   //1670557624
var timestamp = Date.now();
var timestampInSeconds = timestamp / 1000;
console.log(timestampInSeconds)   //1670557624.589
Math.floor(timestampInSeconds)   //1670557624

第二种

js
var date = new Date();
var timestamp = date.getTime() / 1000;
console.log(timestamp)   //1670557571.116
Math.floor(timestamp)   //1670557571
var date = new Date();
var timestamp = date.getTime() / 1000;
console.log(timestamp)   //1670557571.116
Math.floor(timestamp)   //1670557571

时间戳转时间

js
happenTimeFun(num){//时间戳数据处理

         let date = new Date(num);
        //时间戳为10位需*1000,时间戳为13位的话不需乘1000
        let y = date.getFullYear();
        let MM = date.getMonth() + 1;
        MM = MM < 10 ? ('0' + MM) : MM;//月补0
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;//天补0
        let h = date.getHours();
        h = h < 10 ? ('0' + h) : h;//小时补0
        let m = date.getMinutes();
        m = m < 10 ? ('0' + m) : m;//分钟补0
        let s = date.getSeconds();
        s = s < 10 ? ('0' + s) : s;//秒补0
        return y + '-' + MM + '-' + d + ' ' + h + ':' + m+ ':' + s;

}    
happenTimeFun(1628648245);
happenTimeFun(num){//时间戳数据处理

         let date = new Date(num);
        //时间戳为10位需*1000,时间戳为13位的话不需乘1000
        let y = date.getFullYear();
        let MM = date.getMonth() + 1;
        MM = MM < 10 ? ('0' + MM) : MM;//月补0
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;//天补0
        let h = date.getHours();
        h = h < 10 ? ('0' + h) : h;//小时补0
        let m = date.getMinutes();
        m = m < 10 ? ('0' + m) : m;//分钟补0
        let s = date.getSeconds();
        s = s < 10 ? ('0' + s) : s;//秒补0
        return y + '-' + MM + '-' + d + ' ' + h + ':' + m+ ':' + s;

}    
happenTimeFun(1628648245);

优化代码:时间戳转时间

js
/**
 * 时间戳转格式化日期
 * @param {number} timestamp - 时间戳(10位秒级或13位毫秒级)
 * @param {string} format - 输出格式,默认:YYYY-MM-DD HH:mm:ss
 * @returns {string} 格式化后的日期字符串
 */
function formatTimestamp(timestamp, format = 'YYYY-MM-DD HH:mm:ss') {
  // 校验时间戳有效性
  if (isNaN(timestamp) || timestamp < 0) {
    return '无效时间';
  }

  // 自动处理10位秒级时间戳
  const ms = timestamp.toString().length === 10 ? timestamp * 1000 : timestamp;
  const date = new Date(ms);

  // 补零函数(使用padStart替代手动拼接)
  const pad = (n) => n.toString().padStart(2, '0');

  // 提取各时间分量
  const year = date.getFullYear();
  const month = pad(date.getMonth() + 1);
  const day = pad(date.getDate());
  const hour = pad(date.getHours());
  const minute = pad(date.getMinutes());
  const second = pad(date.getSeconds());

  // 支持自定义格式(如:YYYY/MM/DD HH:mm)
  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hour)
    .replace('mm', minute)
    .replace('ss', second);
}
/**
 * 时间戳转格式化日期
 * @param {number} timestamp - 时间戳(10位秒级或13位毫秒级)
 * @param {string} format - 输出格式,默认:YYYY-MM-DD HH:mm:ss
 * @returns {string} 格式化后的日期字符串
 */
function formatTimestamp(timestamp, format = 'YYYY-MM-DD HH:mm:ss') {
  // 校验时间戳有效性
  if (isNaN(timestamp) || timestamp < 0) {
    return '无效时间';
  }

  // 自动处理10位秒级时间戳
  const ms = timestamp.toString().length === 10 ? timestamp * 1000 : timestamp;
  const date = new Date(ms);

  // 补零函数(使用padStart替代手动拼接)
  const pad = (n) => n.toString().padStart(2, '0');

  // 提取各时间分量
  const year = date.getFullYear();
  const month = pad(date.getMonth() + 1);
  const day = pad(date.getDate());
  const hour = pad(date.getHours());
  const minute = pad(date.getMinutes());
  const second = pad(date.getSeconds());

  // 支持自定义格式(如:YYYY/MM/DD HH:mm)
  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hour)
    .replace('mm', minute)
    .replace('ss', second);
}

使用

js
// 10位秒级时间戳
console.log(formatTimestamp(1628648245)); 
// 输出:2021-08-11 10:57:25

// 13位毫秒级时间戳
console.log(formatTimestamp(1693056000000, 'YYYY/MM/DD')); 
// 输出:2023/08/27

// 自定义格式(仅时间部分)
console.log(formatTimestamp(Date.now(), 'HH:mm:ss')); 
// 输出:14:35:42
// 10位秒级时间戳
console.log(formatTimestamp(1628648245)); 
// 输出:2021-08-11 10:57:25

// 13位毫秒级时间戳
console.log(formatTimestamp(1693056000000, 'YYYY/MM/DD')); 
// 输出:2023/08/27

// 自定义格式(仅时间部分)
console.log(formatTimestamp(Date.now(), 'HH:mm:ss')); 
// 输出:14:35:42

程序员小洛文档