cth0

时间戳与日期的转化————小程序

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 + \'日\';
    },

喜欢的话,麻烦给个赞呗。

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-08-09
  • 2021-12-05
  • 2021-09-12
  • 2021-10-28
猜你喜欢
  • 2021-09-11
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2021-08-28
相关资源
相似解决方案