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

前言:

做微信小程序当中,会遇到好多的工具类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] "htmlcode">
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 '数据出错';
 }
 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 = '刚刚发表';
 }
 return result;
}

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

标签:
微信小程序,常用工具类详解,小程序,常用工具类有哪些,微信小程序,常用工具类整理

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“微信小程序 常用工具类详解及实例”

暂无“微信小程序 常用工具类详解及实例”评论...