微信小程序日期/时间格式化
2018-03-23 15:51
621 查看
1、微信自己提供的util中,有一个日期格式化的工具,如果要使用的话,请导入var util = require('../../utils/util.js'),然后关于util.formatTime(new Date)的使用,不要用Date.now(),请老老实实使用 new Date()。2、因为使用的格式是yyyy-mm-dd,所以修改了util中的方法,仿照formatTime写了formatTime2,记得在module.exports中增加对应的定义。unit.jsconst formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')}const formatDate = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate()
return [year, month, day].map(formatNumber).join('-')}const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n}const formatTimeNew = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [hour, minute].map(formatNumber).join(':')}module.exports = { formatTime: formatTime, formatDa
4000
te:formatDate, formatTimeNew:formatTimeNew}
html
<view>{{time}}</view>
js
// 在需要使用的js文件中,导入js
var util = require('../../utils/util.js');
Page({
data: {
time:util.formatTime(new Date())
},
onLoad: function () {
// 调用函数时,传入new Date()参数,返回值是日期和时间
var time = util.formatTime(new Date());
// 再通过setData更改Page()里面的data,动态更新页面的数据
this.setData({
time: time
});
}
})
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')}const formatDate = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate()
return [year, month, day].map(formatNumber).join('-')}const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n}const formatTimeNew = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [hour, minute].map(formatNumber).join(':')}module.exports = { formatTime: formatTime, formatDa
4000
te:formatDate, formatTimeNew:formatTimeNew}
html
<view>{{time}}</view>
js
// 在需要使用的js文件中,导入js
var util = require('../../utils/util.js');
Page({
data: {
time:util.formatTime(new Date())
},
onLoad: function () {
// 调用函数时,传入new Date()参数,返回值是日期和时间
var time = util.formatTime(new Date());
// 再通过setData更改Page()里面的data,动态更新页面的数据
this.setData({
time: time
});
}
})
相关文章推荐
- 微信小程序例子——使用picker实现时间和日期选择框
- 微信小程序 时间格式化(util.formatTime(new Date))详解
- 微信小程序之滚动选择器(时间日期选择器)
- 微信小程序 选择器(时间,日期,地区)实例详解
- 微信小程序 滚动选择器(时间日期)详解及实例代码
- 微信小程序之日期与时间插件
- 013 - 微信小程序开发之选择器 时间选择器 日期选择器 地区选择器
- 微信小程序开发之选择器 时间选择器 日期选择器 地区选择器
- 微信小程序获取系统日期和时间
- Jmail 发送程序,格式化日期时间,FSO文件操作
- 微信小程序日期格式化
- 微信小程序,ios上日期转时间戳出现的问题
- php基础知识 5.流程控制 和格式化日期时间 和终止程序执行
- 微信小程序----日期时间选择器(自定义精确到分秒或时段)(MUI日期时间)
- 微信小程序json时间格式化处理
- 转:【微信小程序常见问题】下拉框选择器设置picker属性。(包括:城市、日期和时间选择器)
- 微信小程序开发之选择器 时间选择器 日期选择器 地区选择器
- 微信小程序-picker组件地区,时间,日期选择
- 微信小程序之picker日期和时间选择器