Common tools and examples of WeChat applets

Source: Internet
Author: User
This article mainly introduces the details of common tool classes for small programs and related information about instances. For more information, see Explanation of common mini-program tools

Preface:

Many tool classes util. js will be used as a small program, which are recorded here for normal use (Ps: It is recommended to view through the directory)

-Get date (format)

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}

-Get the 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) {// invalid operation return 'data error';} 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) + 'years ago ';} else if (monthC> = 1) {result = parseInt (monthC) + 'months ago ';} else if (weekC> = 1) {result = parseInt (weekC) + 'weeks ago';} else if (dayC> = 1) {result = parseInt (dayC) + 'day before ';} else if (hourC> = 1) {result = parseInt (hourC) + 'hour before ';} else if (minC> = 5) {result = parseInt (minC) + 'minute ago ';} else {result = 'Just posted';} return result ;}

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For more details about common tools and examples of small programs, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.