小程序 - 时间戳、input取值 小方法(2.5)
作者:互联网
文章目录: | |
---|---|
1、时间戳的具体用法; | |
2、input 取、传值的方法; |
1、时间戳:
util.js:
//日期 时间戳转换
function createData(dateTimeStamp) {
var result;
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
//13位的时间戳, 如果不是13位的, 就要乘1000,就像这样 let date = new Date(timestamp*1000)
var timestamp = Date.parse(new Date());
//timestamp = timestamp / 1000; // 当前时间戳转10位
var n = timestamp;
var now = new Date(n);
//console.log("当前时间戳为:" + n); // 13位
var diffValue = now - (dateTimeStamp * 1000); //dateTimeStamp 打印出来为10位
if (diffValue < 0) { return; }
var monthC = diffValue / month;
var weekC = diffValue / (7 * day);
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;
if (monthC >= 1) {
result = "" + parseInt(monthC) + "月前";
}
else if (weekC >= 1) {
result = "" + parseInt(weekC) + "周前";
}
else if (dayC >= 1) {
result = "" + parseInt(dayC) + "天前";
}
else if (hourC >= 1) {
result = "" + parseInt(hourC) + "小时前";
}
else if (minC >= 1) {
result = "" + parseInt(minC) + "分钟前";
} else
result = "刚刚";
return result;
}
//让函数出去
module.exports = {
formatTime: formatTime
}
页面中调用函数
var app = getApp();
var Api = require("../../../utils/util.js");
Page({
//时间戳
var datas = res.data.data.receiveLogs; //接口中的数据
for (let i = 0; i < datas.length; i++) {
datas[i]["createTime"] = Api.createData(datas[i]["createTime"]) //createTime 接口返回的时间戳
}
})
2、拿到input中的值:
<view class='weui-cell'>
<view class='weui-cell-bd'>
<input type='text' confirm-type="done" placeholder="{{title}}" maxlength="15" class='m-int' style='text-align: left;' bindinput='textWdInput'></input>
</view>
</view>
<script>
onl oad: function (options) {
var that = this
that.setData({
tokenType: options.tokenType, //币的名称
title: "我有" + options.tokenType + "福包,快来领取吧!"
})
},
// 标题
textWdInput: function(e) {
var that = this
var title_item = e.detail.value.replace(/\s+/g, ''); //清除空字符串
that.setData({
verificationText: false,
title: title_item
})
console.log("标题:" + title_item)
},
</script>
标签:title,timestamp,diffValue,result,var,input,取值,day,2.5 来源: https://blog.csdn.net/weixin_42675488/article/details/89419012