時間戳與日期的轉化————小程序
const app = getApp()
const now = new Date();
const month = now.getMonth() + 1 *//⽉份需要+1*
const day = now.getDate()
var timestamp = Date.parse(new Date());
//把當前日期轉化為時間戳數字
timestamp = timestamp / 1000;
//得出具體當前時間的時間戳
console.log("當前時間戳為:" + timestamp);
console.log("當前日期為:" + now);
公式逆轉下就是時間戳求日期了。(數字轉日期,排版什么的要調下)
①/** 時間戳轉日期 格式2017-01-20 00:00:00*/
getLocalTime: function (ns) {
//needTime是整數,否則要parseInt轉換
var time = new Date(parseInt(ns) * 1000); //根據情況*1000
var y = time.getFullYear();
var m = time.getMonth() + 1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y + '-' + this.add0(m) + '-' + this.add0(d) + ' ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s);
},
//小於10的補零操作
add0:function(m){
return m < 10 ? '0' + m : m
},
② /**時間戳轉日期 格式2018年01月01日*/
getChaYMD: function (ns) {
var allStr = this.getLocalTime(ns);
var year = allStr.substr(0, 4);
var month = allStr.substr(5, 2);
var day = allStr.substr(8, 2);
return year + '年' + month + '月' + day + '日';
},
喜歡的話,麻煩給個贊唄。