12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- 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}`;
- };
- //获得对应时间格式
- const formatTime2 = (time, type) => {
- var date = new Date(time);
- var format = type || "YYYY-MM-DD HH:NN:SS";
- const year = date.getFullYear();
- const month =
- date.getMonth() + 1 < 10
- ? "0" + (date.getMonth() + 1)
- : date.getMonth() + 1;
- const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
- const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
- const minute =
- date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
- const second =
- date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
- format.indexOf("YYYY") > -1 ? (format = format.replace("YYYY", year)) : "";
- format.indexOf("MM") > -1 ? (format = format.replace("MM", month)) : "";
- format.indexOf("DD") > -1 ? (format = format.replace("DD", day)) : "";
- format.indexOf("HH") > -1 ? (format = format.replace("HH", hour)) : "";
- format.indexOf("NN") > -1 ? (format = format.replace("NN", minute)) : "";
- format.indexOf("SS") > -1 ? (format = format.replace("SS", second)) : "";
- return format;
- };
- module.exports = {
- formatTime,
- formatTime2
- };
|