WXS (WeiXin Script) is a scripting language for small programs. WXS is a different language from JavaScript and has its own syntax, which is not consistent with JavaScript. It includes many daily used JavaScript functions, which cannot be used in wxs. Recently, when I was making a list, I was involved in time formatting operations. I encountered this problem. The formatting tool function I wrote before was directly copied to the wxs file of the applet project, and the function could not be executed normally. These include the following errors
1. Timestamp to date To process dates in wxs, you need to use getDate(time) instead of new Date(). In the wxs file var filter = { formatNumber: function (n) { n = n.toString() return n[1] ? n : '0' + n }, parseTime: function (time, type) { if (time == null || type == '') { return '' } if (arguments.length === 0) { return null } var date = getDate(time); // In wxs, you cannot use new Date() to process dates console.log("date", date); var y = date.getFullYear(); var m = filter.formatNumber(date.getMonth() + 1); var d = filter.formatNumber(date.getDate()); var h = filter.formatNumber(date.getHours()); var i = filter.formatNumber(date.getMinutes()); var s = filter.formatNumber(date.getSeconds()); var a = filter.formatNumber(date.getDay()); var time_str = ""; if (type == 'month') { time_str = y + '-' + m; } else if (type == 'date') { time_str = y + '-' + m + '-' + d; } else if (type == 'datetime') { time_str = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s; } else if (type == 'onlyMonth') { time_str = m; } else if (type == 'onlyYear') { time_str = y; } return time_str }, } module.exports = { parseTime: filter.parseTime, } Use in wxml <wxs module="filters" src="../../../filters/filter.wxs"></wxs> <text>{{filters.parseTime(time,'date')}}</text> 2. Convert UTC to Beijing Time UTC time is 8 hours later than Beijing time. On Apple phones, you need to remove the "Z" before processing the time. var filter = { formatNumber: function (n) { n = n.toString() return n[1] ? n : '0' + n }, parseTime: function (time, type) { if (time == null || time == '') { return '' } if (arguments.length === 0) { return null } var date; if (typeof time === 'object') { date = time } else { if (('' + time).length === 10) { time = parseInt(time) * 1000 } else { time = time.replace("Z", " ").replace(getRegExp('-', 'g'), "/")//Remove Z, compatible with Apple phones var ts = time.split('T') var t1 = ts[0] var t2 = ts[1].split('.')[0] time = t1 + " " + t2 time = getDate(time).getTime() + 8 * 3600000; //utc time is 8 hours different from Beijing time} date = getDate(time) //Cannot use new Date() } var y = date.getFullYear(); var m = filter.formatNumber(date.getMonth() + 1); var d = filter.formatNumber(date.getDate()); var h = filter.formatNumber(date.getHours()); var i = filter.formatNumber(date.getMinutes()); var s = filter.formatNumber(date.getSeconds()); var a = filter.formatNumber(date.getDay()); var time_str = ""; if (type == 'month') { time_str = y + '-' + m; } else if (type == 'date') { time_str = y + '-' + m + '-' + d; } else if (type == 'datetime') { time_str = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s; } else if (type == 'onlyMonth') { time_str = m; } else if (type == 'onlyYear') { time_str = y; } return time_str }, } module.exports = { parseTime: filter.parseTime, } This is the end of this article about the implementation example of WeChat applet wxs date and time processing. For more relevant applet wxs date and time content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of Linux lsof command usage
>>: Solution to MySQL restarting automatically
There are two ways to expose container ports in d...
When using a virtual machine, you may find that t...
Table of contents Install Basic configuration of ...
MySQL is a relational database management system ...
After the user logs out, if the back button on the...
Operating system: windowns10_x64 Python version: ...
This article uses examples to explain the princip...
After installing centos 8, the following error wi...
While the paperless world has not yet emerged, mo...
Error message: Job for mysqld.service failed beca...
Preface [root@localhost ~]# cat /etc/fstab # # /e...
MySQL regular sorting, custom sorting, and sortin...
Table of contents Asynchronous traversal Asynchro...
Table of contents Scene Introduction Plugin Imple...
Generally, when we use a table, we always give it...