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 use WeChat Mini Programs's formatting method

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the relevant knowledge of how to use WeChat Mini Programs's formatting method. The content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this WeChat Mini Programs formatting method. Let's take a look.

Util.js

This code contains three paragraphs, one by one according to the reference relationship.

Formatted numeric function

Function formatNumber (n) {

N = n.toString ()

Return n [1]? N:'0' + n

}

This function is used to convert date and time numbers into a string, preceded by 0 if the string is a character.

Format date / time function

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 (':')

}

This function converts the date data containing time information into a string. Its main function is implemented in the return statement.

This statement first calls the map method, uses the formatNumber function to convert the year, month, and day number array into a string array, and then calls the join method to concatenate the elements in the string array with'/'.

Next, call the map method, use the formatNumber function to convert the hour-minute-second number array into a string array, and then call the join method to concatenate the elements in the string array with':'.

Finally, the date string and the time string are concatenated and returned.

External exposure interface

Module.exports = {

FormatTime: formatTime

}

Util.js is a separate js file module. The module can expose the interface through module.exports (). In this example, the formatTime function is exposed to other modules as formatTime.

Logs.js

/ / logs.js

Var util = require ('.. /.. / utils/util.js')

Page ({

Data: {

Logs: []

}

OnLoad: function () {

This.setData ({

Logs: (wx.getStorageSync ('logs') | | []) .map (function (log) {

Return util.formatTime (new Date (log))

})

})

}

})

To use the format function exposed in util.js, first use require (path) to introduce the common code into this file.

In the life cycle function onLoad on this page, you first extract the logs array from the Mini Program temporary storage, and then call the formatTime function in util.js on each of its elements to transform the date data one by one. Finally, the result is generated into a string array and set to the logs array in the page data for display.

Log data is formed when Mini Program is started and is displayed on the log page.

This is the end of the article on "how to use WeChat Mini Programs's formatting method". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use WeChat Mini Programs formatting method". If you want to learn more knowledge, you are welcome to follow 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

Internet Technology

Wechat

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

12
Report