移动端V1.0
This commit is contained in:
40
utils/getCurrentDateTime.js
Normal file
40
utils/getCurrentDateTime.js
Normal file
@@ -0,0 +1,40 @@
|
||||
// 封装获取当前时间年月日的函数
|
||||
export function getCurrentDateTime(format = 'yyyy-MM-dd') {
|
||||
const now = new Date();
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, '0'); // getMonth() 返回的月份是从 0 开始的
|
||||
const day = String(now.getDate()).padStart(2, '0');
|
||||
|
||||
// 根据传入的 format 返回不同的时间格式
|
||||
switch (format) {
|
||||
case 'yyyy':
|
||||
return year;
|
||||
case 'MM':
|
||||
return month;
|
||||
case 'dd':
|
||||
return day;
|
||||
default:
|
||||
// 默认返回 'yyyy-MM-dd' 格式
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
}
|
||||
export function getCurrentTime(format = 'yyyy-MM-dd HH:mm:ss') {
|
||||
const now = new Date();
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的
|
||||
const day = String(now.getDate()).padStart(2, '0');
|
||||
const hours = String(now.getHours()).padStart(2, '0');
|
||||
const minutes = String(now.getMinutes()).padStart(2, '0');
|
||||
const seconds = String(now.getSeconds()).padStart(2, '0');
|
||||
|
||||
// 根据传入的 format 字符串拼接日期时间
|
||||
let formattedDateTime = format
|
||||
.replace('yyyy', year)
|
||||
.replace('MM', month)
|
||||
.replace('dd', day)
|
||||
.replace('HH', hours)
|
||||
.replace('mm', minutes)
|
||||
.replace('ss', seconds);
|
||||
|
||||
return formattedDateTime;
|
||||
}
|
Reference in New Issue
Block a user