JS 时间常用案例

158 阅读3分钟
以下是一些 js 中常见的时间操作案例,这些案例涵盖了日期和时间的获取、格式化、计算等操作,帮助你在实际开发中处理时间相关的问题。

1. 获取当前日期和时间

// 输出当前日期和时间
const now = new Date();
console.log(now); // 2024-08-30T12:34:56.789Z
// 输出当前格式化时间
const formatDate = new Date().toLocaleString()
console.log(formatDate) //  '2024/9/13 18:02:11'
  • 说明new Date() 创建一个表示当前日期和时间的 Date 对象。

2. 获取当前时间戳

const timestamp = Date.now();
console.log(timestamp); // 输出当前时间戳,例如:1725023696789
  • 说明Date.now() 返回自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数。

3. 获取单独的年份、月份、日期

const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1; // 月份是从 0 开始的,所以需要加 1
const date = now.getDate();

console.log(`Year: ${year}, Month: ${month}, Date: ${date}`); // Year: 2024, Month: 8, Date: 30
  • 说明:通过 getFullYear()getMonth()getDate() 方法可以分别获取年份、月份和日期。

4. 获取当前的小时、分钟、秒

const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();

console.log(`Time: ${hours}:${minutes}:${seconds}`); // Time: 12:34:56
  • 说明:通过 getHours()getMinutes()getSeconds() 方法可以分别获取小时、分钟和秒。

5. 将字符串解析为日期

const dateStr = '2024-08-30';
const date = new Date(dateStr);

console.log(date); // 输出日期对象,例如:2024-08-30T00:00:00.000Z
  • 说明new Date() 可以将标准的日期字符串解析为日期对象。

6. 将时间戳转换为日期

const timestamp = 1725023696789;
const date = new Date(timestamp);

console.log(date); // 输出对应的日期对象
  • 说明:通过 new Date(timestamp) 可以将时间戳转换为日期对象。

7. 设置日期和时间

const date = new Date();
date.setFullYear(2025);
date.setMonth(11); // 11 表示 12 月
date.setDate(25);
date.setHours(10);
date.setMinutes(30);
date.setSeconds(15);

console.log(date); // 输出设置后的日期对象,例如:2025-12-25T10:30:15.000Z
  • 说明setFullYear()setMonth()setDate()setHours()setMinutes()setSeconds() 方法可以分别设置日期和时间的各个部分。

8. 增加或减少日期

javascript
复制代码
const date = new Date('2024-08-30');
date.setDate(date.getDate() + 10); // 增加 10 天

console.log(date); // 输出新日期,例如:2024-09-09
  • 说明:可以使用 setDate() 方法对日期进行加减操作,从而得到新的日期。

9. 获取当前星期几

function getDayOfWeek() {
    const daysOfWeek = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    const today = new Date();
    const day = today.getDay(); // getDay() 返回 0(星期日)到 6(星期六)

    return daysOfWeek[day];
}

// 示例使用
console.log(getDayOfWeek()); // 输出例如 "星期五"
  • 说明getDay() 方法返回一个 0 到 6 的值,表示周日到周六。

10. 格式化日期为自定义格式

function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
    const padZero = (number) => number < 10 ? '0' + number : number;

    const day = padZero(date.getDate());
    const month = padZero(date.getMonth() + 1); // 月份从 0 开始,所以要加 1
    const year = date.getFullYear();
    const hours = padZero(date.getHours());
    const minutes = padZero(date.getMinutes());
    const seconds = padZero(date.getSeconds());

    return format
        .replace('YYYY', year)
        .replace('MM', month)
        .replace('DD', day)
        .replace('HH', hours)
        .replace('mm', minutes)
        .replace('ss', seconds);
}

// 示例使用
const now = new Date();
console.log(formatDate(now)); // 默认格式输出 "2024-08-30 15:07:05"
console.log(formatDate(now, 'YYYY/MM/DD')); // 自定义格式输出 "2024/08/30"