小程序模板網(wǎng)

微信小程序常用工具類封裝

發(fā)布時間:2017-11-29 18:23 所屬欄目:小程序開發(fā)教程

做微信小程序當(dāng)中,會遇到好多的工具類util.js,這里記載下來以便平常使用(Ps:建議通過目錄查看)-獲取日期(格式化)function formatTime(date) { var year = ...

 
 
 

微信小程序 常用工具類詳解

前言:

做微信小程序當(dāng)中,會遇到好多的工具類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
}


-獲取動態(tài)更新時間

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 '數(shù)據(jù)出錯';
 }
 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) + '年以前';
 }else 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 >= 5){
  result = parseInt(minC) + '分鐘前';
 }else{
  result = '剛剛發(fā)表';
 }
 return result;
}


感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!



易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://22321a.com/wxmini/doc/course/17996.html 復(fù)制鏈接 如需定制請聯(lián)系易優(yōu)客服咨詢:800182392 點擊咨詢
QQ在線咨詢