首页 资源列表 文章列表

uni-app 实时获取当前时间(时分秒)日期时间处理

格式化时间戳

dateFormat(time) {

   let date = new Date(time);

   let year = date.getFullYear();

   // 在日期格式中,月份是从0开始的,因此要加0,使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05

   let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;

   let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

   let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();

   let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();

   let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

   // 拼接

   return year + "年" + month + "月" + day + "日" + hours + ":" + minutes + ":" + seconds;

   // return year + "-" + month + "-" + day;

}


日期转时间戳

let dateString = '2023-04-01'; // 示例日期字符串

let timestamp = Date.parse(dateString);

console.log(timestamp); // 输出时间戳


获取当前时间戳

let currentDate = new Date(); // 创建Date对象,返回当前日期和时间

let currentTime = currentDate.getTime(); // 获取当前时间的时间戳

console.log(currentTime);