12345678910111213141516 |
- export function getCurrentTime(format) {
- const date = new Date();
- const year = date.getFullYear(); // 年份
- const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份(注意需要加1)
- const day = date.getDate().toString().padStart(2, '0'); // 天数
- const hours = date.getHours().toString().padStart(2, '0'); // 小时数
- const minutes = date.getMinutes().toString().padStart(2, '0'); // 分钟数
- const seconds = date.getSeconds().toString().padStart(2, '0'); // 秒数
- if (format === 'yyyy-MM-dd HH:mm:ss') {
- return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // 返回格式为YYYY-MM-DD HH:mm:ss的时间字符串
- } else if (format === 'yyyy-MM-dd') {
- return `${year}-${month}-${day}`; // 返回格式为YYYY-MM-DD的时间字符串
- }
- }
|