Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with the date and time of WeChat Mini Programs wxs

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge about how to deal with the date and time of WeChat Mini Programs wxs. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

WXS (WeiXin Script) is a set of scripting languages of Mini Program. Wxs and javascript are different languages, have their own syntax, and are not consistent with javascript. This includes many everyday javascript functions that cannot be used in wxs as well. Recently, when making a list, it involved a time formatting operation. Encountered this problem, the previously written formatting tool function is directly copied to the Mini Program project wxs file, the function can not be executed normally. This includes the following mistakes

Regular expressions are used differently in the replace function of a string and cannot be declared to be used directly using var a = / [0-9] /. The correct way is var reg = getRegExp ("-", "g")

Getting the current time is not available through new Date (), but through the getDate method.

GetDate ('2018-12-12') can get the time of the date type for the corresponding date.

1. Time stamp to date

Date processing in wxs requires the use of getDate (time), but not 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); / / new Date () cannot be used in wxs to process date 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

{{filters.parseTime (time, "date")} 2. UTC transfer time to Beijing

UTC time is 8 hours later than Beijing time, and processing time needs to be removed after "Z" is removed on iPhone.

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 iPhone var ts = time.split ("T") var T1 = ts [0] var T2 = ts [1] .split (".") [0] time = T1 + "+ T2 time = getDate (time). GetTime () + 8 * 3600000 / / the difference between utc time and Beijing time is 8 hours} 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,} these are all the contents of the article "how to deal with the date and time of WeChat Mini Programs wxs". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report