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!