微信小程序获取系统时间、时间戳、时间时间戳加减


   
   
  1. //获取当前时间戳
  2. var timestamp = Date.parse( new Date());
  3. timestamp = timestamp / 1000;
  4. console.log( "当前时间戳为:" + timestamp);
  5. //获取当前时间
  6. var n = timestamp * 1000;
  7. var date = new Date(n);
  8. //年
  9. var Y = date.getFullYear();
  10. //月
  11. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  12. //日
  13. var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  14. //时
  15. var h = date.getHours();
  16. //分
  17. var m = date.getMinutes();
  18. //秒
  19. var s = date.getSeconds();
  20. console.log( "当前时间:" +Y+M+D+h+ ":"+m+ ":"+s);
  21. //转换为时间格式字符串
  22. console.log(date.toDateString());
  23. console.log(date.toGMTString());
  24. console.log(date.toISOString());
  25. console.log(date.toJSON());
  26. console.log(date.toLocaleDateString());
  27. console.log(date.toLocaleString());
  28. console.log(date.toLocaleTimeString());
  29. console.log(date.toString());
  30. console.log(date.toTimeString());
  31. console.log(date.toUTCString());


   
   
  1. //时间、时间戳加减 以加一天举例,聪明的你肯定触类旁通
  2. //加一天的时间戳:
  3. var tomorrow_timetamp = timestamp + 24 * 60 * 60;
  4. //加一天的时间:
  5. var n_to = tomorrow_timetamp * 1000;
  6. var tomorrow_date = new Date(n_to);
  7. //加一天后的年份
  8. var Y_tomorrow = tomorrow_date.getFullYear();
  9. //加一天后的月份
  10. var M_tomorrow = (tomorrow_date.getMonth() + 1 < 10 ? '0' + (tomorrow_date.getMonth() + 1) : tomorrow_date.getMonth() + 1);
  11. //加一天后的日期
  12. var D_tomorrow = tomorrow_date.getDate() < 10 ? '0' + tomorrow_date.getDate() : tomorrow_date.getDate();
  13. //加一天后的时刻
  14. var h_tomorrow = tomorrow_date.getHours();
  15. //加一天后的分钟
  16. var m_tomorrow = tomorrow_date.getMinutes();
  17. //加一天后的秒数
  18. var s_tomorrow = tomorrow_date.getSeconds();




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值