教程 date-fns tutorial

roundToNearestHours

将给定日期舍入到最接近的小时。

javascript
import { roundToNearestHours } from "date-fns";

// 示例日期
const date = new Date(2024, 0, 1, 12, 30, 0); // 2024年1月1日 12:30:00

// 将日期舍入到最接近的 2 小时
const roundedDate = roundToNearestHours(date, { nearestTo: 2 });

console.log("舍入后的日期:", roundedDate);
// 舍入后的日期:Sun Jan 01 2024 14:00:00