search
HomeWeChat AppletMini Program DevelopmentCode implementation of formatting time in WeChat applet

The content of this article is about the code implementation of formatting time in WeChat applet. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. formatTime: Data type 2.formatNumber1: Long type

util.js

----------
//两种方式 1.formatTime 传入参数 Date 返回:年/月/日
const formatTime = date => {  
const year = date.getFullYear()  
const month = date.getMonth() + 1
  const day = date.getDate()  
  const hour = date.getHours()  
  const minute = date.getMinutes()  
  const second = date.getSeconds()  
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}const formatNumber = n => {
  n = n.toString()  
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  formatTime1: formatTime1
}
function formatTime1(n){
  n = n.toString()  
  return n[1] ? n : '0' + n
}
// 2.formatTime1传入参数 (时间戳,格式:如YYYY -MM-DD)
//返回:年-月-日(自定义)
function formatTime1(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];  
  var returnArr = [];  
  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber1(date.getMonth() + 1));
  returnArr.push(formatNumber1(date.getDate()));

  returnArr.push(formatNumber1(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber1(date.getSeconds()));  
  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }  
  return format;
}

Related recommendations:

JSON formatting and serialize serialization - Lu Xiaolu

php formatting tool Beautify PHP small BUG

The above is the detailed content of Code implementation of formatting time in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools