### [微信小程序实例:实现系统时间、时间戳时间以及时间戳加减的获取代码](https://www.prosaas.cn/article/17144) **Published:** 2018-08-11T09:28:48 **Author:** 5201314 **Excerpt:** 本篇文章给大家带来的内容是关于微信小程序实例:实现系统时间、时间戳时间以及时间戳加减的获取代码 ,有一定的参考… 本篇文章给大家带来的内容是关于微信小程序实例:实现系统时间、时间戳时间以及时间戳加减的获取代码 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 //获取当前时间戳 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(); 相关推荐: 小程序:防止点击遮罩层后遮罩层下面也反应的解决方法 微信小程序实例:分享给一个人还是分享到群的判断代码 以上就是微信小程序实例:实现系统时间、时间戳时间以及时间戳加减的获取代码的详细内容,更多请关注知企PROSAAS其它相关文章! **Tags:** 微信小程序 **Categories:** 后端开发 ---