首页  >  文章  >  微信小程序  >  微信小程序的常用工具类详解及实例

微信小程序的常用工具类详解及实例

高洛峰
高洛峰原创
2017-02-25 09:22:311994浏览

微信小程序 常用工具类详解

前言:

做微信小程序当中,会遇到好多的工具类util.js,这里记载下来以便平常使用 (Ps:建议通过目录查看)

-获取日期(格式化)

function formatTime(date) {
 var year = date.getFullYear()
 var month = date.getMonth() + 1
 var 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
}

-获取动态更新时间

function getDateDiff (dateTimeStamp) {
 var minute = 1000 * 60;
 var hour = minute * 60;
 var day = hour * 24;
 var halfamonth = day * 15;
 var month = day * 30;
 var year = day * 365;
 var now = new Date().getTime();
 var diffValue = now - dateTimeStamp;
 if(diffValue < 0){
  //非法操作
  return &#39;数据出错&#39;;
 }
 var yearC = diffValue / year;
 var monthC = diffValue / month;
 var weekC = diffValue / (7 * day);
 var dayC = diffValue / day;
 var hourC = diffValue / hour;
 var minC = diffValue / minute;
 if(yearC >= 1){
  result = parseInt(yearC) + &#39;年以前&#39;;
 }else if(monthC >= 1){
  result = parseInt(monthC) + &#39;个月前&#39;;
 }else if(weekC >= 1){
  result = parseInt(weekC) + &#39;星期前&#39;;
 }else if(dayC >= 1){
  result = parseInt(dayC) + &#39;天前&#39;;
 }else if(hourC >= 1){
  result = parseInt(hourC) + &#39;小时前&#39;;
 }else if(minC >= 5){
  result = parseInt(minC) + &#39;分钟前&#39;;
 }else{
  result = &#39;刚刚发表&#39;;
 }
 return result;
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多微信小程序的常用工具类详解及实例相关文章请关注PHP中文网!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn