Home  >  Article  >  WeChat Applet  >  Code implementation of formatting time in WeChat applet

Code implementation of formatting time in WeChat applet

不言
不言Original
2018-08-16 17:17:178335browse

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