Home WeChat Applet Mini Program Development Detailed explanations and examples of commonly used tools in WeChat mini programs

Detailed explanations and examples of commonly used tools in WeChat mini programs

Feb 25, 2017 am 09:22 AM

Detailed explanation of commonly used tool classes in WeChat mini programs

Foreword:

When making WeChat mini programs, you will encounter many tool classes util.js. Record it here for daily use (Ps: It is recommended to view it through the directory)

-Get the date (formatting)

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
}
Copy after login

-Get dynamic update time

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;
}
Copy after login

Thank you for reading, I hope it can help everyone, thank you for your support of this site!

For more detailed explanations and examples of commonly used tools in WeChat mini programs, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)