The article directories
- js
- html
- Results show that
js
formatTime.js:
/* * The timestamp format is displayed in the normal time format */
/** Format the date, such as month, day, hour, minute, and second, with two digits *@param {Object} n* /
function formatNumber (n) {
n = n.toString()
return n[1]? n :'0' + n;
}
/** The number argument is the millisecond timestamp, and the format is the date format to convert to *@param {Object} number
* @param {Object} format*/ Use formatTime(1545903266795, 'Y year M month D day h: M :s') or formatTime(1545903266795, 'y-m-d h: M :s')
function formatTime (number, format) {
let time = new Date(number)
let newArr = []
let formatArr = ['Y'.'M'.'D'.'h'.'m'.'s']
newArr.push(time.getFullYear())
newArr.push(formatNumber(time.getMonth() + 1))
newArr.push(formatNumber(time.getDate()))
newArr.push(formatNumber(time.getHours()))
newArr.push(formatNumber(time.getMinutes()))
newArr.push(formatNumber(time.getSeconds()))
for (let i in newArr) {
format = format.replace(formatArr[i], newArr[i])
}
return format;
}
Copy the code
html
/ / reference
<script src=".. /.. /js/formatTime.js" type="text/javascript" charset="utf-8"></script>
Copy the code
/ / use
var startTime = data.data.rows[i].startTime;// This is a timestamp format from the background
var startTime1 = formatTime(startTime,Y year M month D day h: M :s);
var startTime2 = formatTime(startTime,'Y-M-D h:m:s');
alert(startTime1+'-'+startTime2);
Copy the code