生命的意义在于折腾

微信小程序wxs语法格式化时间

JavaScript

1/*
2 * format string // yyyy-MM-dd HH:mm:ss 时间格式
3 * timestamp  number //时间
4 * Auth: WANGJIAN
5 */
6var Format = function Format(format, timestamp) {
7  if (!format) {
8    format = "yyyy-MM-dd hh:mm:ss";
9  }
10  timestamp = parseInt(timestamp)*1000;
11  var realDate = getDate(timestamp);
12  function timeFormat(num) {
13    return num < 10 ? '0' + num : num;
14  }
15  var date = [
16    ["M+", timeFormat(realDate.getMonth() + 1)],
17    ["d+", timeFormat(realDate.getDate())],
18    ["h+", timeFormat(realDate.getHours())],
19    ["m+", timeFormat(realDate.getMinutes())],
20    ["s+", timeFormat(realDate.getSeconds())],
21    ["q+", Math.floor((realDate.getMonth() + 3) / 3)],
22    ["S+", realDate.getMilliseconds()],
23  ];
24  var reg1 = getRegExp("(y+)", "i").exec(format);
25  if (reg1) {
26    format = format.replace(reg1[1], (realDate.getFullYear() + '').substring(4 - reg1[1].length));
27  }
28  for (var i=0;i<date.length;i++) {
29    var k = date[i][0];
30    var v = date[i][1];
31
32    var reg2 = getRegExp("(" + k + ")").exec(format);
33    if (reg2) {
34      format = format.replace(reg2[1], reg2[1].length == 1
35        ? v : ("00" + v).substring(("" + v).length));
36    }
37  }
38  return format;
39};
40module.exports = {
41  Format: Format
42};
阅读量:3709发布日期:2021-06-11 16:50:10

博客描述

用于小程序页面渲染,格式化时间

留言板