汉字市城乡建设规划局官方网站,阿里网站怎样做seo,做网站前台和后台是什么,新闻头条 今天一、直接使用方式
在小程序中获取当前系统日期和时间#xff0c;可直接拿来使用的常用的日期格式
//1. 当前日期 YYYY-MM-DDnew Date().toISOString().substring(0, 10)new Date().toJSON().substring(0, 10)//2. 当前日期 YYYY/MM/DDnew Date().toLocaleDateString()//3.… 一、直接使用方式
在小程序中获取当前系统日期和时间可直接拿来使用的常用的日期格式
//1. 当前日期 YYYY-MM-DDnew Date().toISOString().substring(0, 10)new Date().toJSON().substring(0, 10)//2. 当前日期 YYYY/MM/DDnew Date().toLocaleDateString()//3. 当前时间 HH:mm:ss
new Date().toTimeString().substring(0,8)//4. 当前日期时间 YYYY-MM-DD HH:mm:ss
new Date().toJSON().substring(0, 10) new Date().toTimeString().substring(0,8)
二、封装utils方式
2.1 utils代码
function formatTime(date) {var year date.getFullYear()var month date.getMonth() 1var day date.getDate()var hour date.getHours()var minute date.getMinutes()var second date.getSeconds()return [year, month, day].map(formatNumber).join(/) [hour, minute, second].map(formatNumber).join(:)
}function formatNumber(n) {n n.toString()return n[1] ? n : 0 n
}//此处声明几个方法就写几个,如上面定义了formatTime 写法如下
module.exports {formatTime: formatTime
}
一般放入utils工具类 2.2 utils调用 参考代码
// 在需要使用的js文件中导入js
var util require(../../utils/util.js);
Page({data: {},onLoad: function () {// 调用函数时传入new Date()参数返回值是日期和时间let time util.formatTime(new Date());// 再通过setData更改Page()里面的data动态更新页面的数据this.setData({time: time});}}) 示例