首页 > 微信 > 详细

微信小程序获取当前时间戳、获取当前时间、时间戳加减

时间:2019-10-28 14:27:16      阅读:818      评论:0      收藏:0      [点我收藏+]
//获取当前时间戳  
    var timestamp = Date.parse(new Date());  
    timestamp = timestamp / 1000;  
    console.log("当前时间戳为:" + timestamp);  
  
//获取当前时间  
    var n = timestamp * 1000;  
    var date = new Date(n);  
    //
    var Y = date.getFullYear();  
    //
    var M = (date.getMonth() + 1 < 10 ? 0 + (date.getMonth() + 1) : date.getMonth() + 1);  
    //
    var D = date.getDate() < 10 ? 0 + date.getDate() : date.getDate();  
    //
    var h = date.getHours();  
    //
    var m = date.getMinutes();  
    //
    var s = date.getSeconds();  
    
    console.log("当前时间:" +Y+M+D+h+":"+m+":"+s);  
      
//转换为时间格式字符串  
    console.log(date.toDateString());  
  
    console.log(date.toGMTString());  
  
    console.log(date.toISOString());  
    
    console.log(date.toJSON());  
      
    console.log(date.toLocaleDateString());  
  
    console.log(date.toLocaleString());  
    
    console.log(date.toLocaleTimeString());  
     
    console.log(date.toString());  
     
    console.log(date.toTimeString());  
     
    console.log(date.toUTCString());  
 
//时间、时间戳加减 以加一天举例
    //加一天的时间戳:  
    var tomorrow_timetamp = timestamp + 24 * 60 * 60;  
    //加一天的时间:  
    var n_to = tomorrow_timetamp * 1000;  
    var tomorrow_date = new Date(n_to);  
    //加一天后的年份  
    var Y_tomorrow = tomorrow_date.getFullYear();  
    //加一天后的月份  
    var M_tomorrow = (tomorrow_date.getMonth() + 1 < 10 ? 0 + (tomorrow_date.getMonth() + 1) : tomorrow_date.getMonth() + 1);  
    //加一天后的日期  
    var D_tomorrow = tomorrow_date.getDate() < 10 ? 0 + tomorrow_date.getDate() : tomorrow_date.getDate();  
    //加一天后的时刻  
    var h_tomorrow = tomorrow_date.getHours();  
    //加一天后的分钟  
    var m_tomorrow = tomorrow_date.getMinutes();  
    //加一天后的秒数  
    var s_tomorrow = tomorrow_date.getSeconds(); 

 

微信小程序获取当前时间戳、获取当前时间、时间戳加减

原文:https://www.cnblogs.com/wuliujun521/p/11751477.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!