Moment.js là một thư viện JavaScript phổ biến giúp thao tác với ngày tháng và thời gian dễ dàng hơn. Tuy nhiên, Moment.js hiện đã ngừng phát triển chính thức, và thư viện này vẫn được sử dụng rộng rãi do tính tiện dụng và đơn giản trong việc làm việc với ngày tháng.
Dưới đây là một số thao tác cơ bản với Dates và Times sử dụng Moment.js:
Bạn có thể tạo một đối tượng Moment với thời gian hiện tại hoặc một thời gian cụ thể.
var now = moment(); console.log(now.format());
var specificDate = moment("2023-09-22", "YYYY-MM-DD"); console.log(specificDate.format());
Bạn cũng có thể tạo từ các chuỗi thời gian với nhiều định dạng khác nhau:
var customDate = moment("2023-09-22 14:30", "YYYY-MM-DD HH:mm"); console.log(customDate.format());
Moment.js
cho phép định dạng ngày tháng theo nhiều cách khác nhau.
var now = moment(); console.log(now.format("YYYY-MM-DD")); // 2024-09-22 console.log(now.format("DD/MM/YYYY")); // 22/09/2024 console.log(now.format("MMMM Do YYYY")); // September 22nd 2024
console.log(now.format("HH:mm:ss")); // 14:30:45 console.log(now.format("hh:mm A")); // 02:30 PM
var now = moment(); var newDate = now.add(7, 'days'); // Thêm 7 ngày console.log(newDate.format("YYYY-MM-DD"));
Các đơn vị thời gian có thể sử dụng:
seconds
(giây)minutes
(phút)hours
(giờ)days
(ngày)months
(tháng)years
(năm)var now = moment(); var newDate = now.subtract(2, 'months'); // Giảm 2 tháng console.log(newDate.format("YYYY-MM-DD"));
var date1 = moment("2023-09-22"); var date2 = moment("2023-09-30"); console.log(date1.isBefore(date2)); // true console.log(date1.isAfter(date2)); // false
console.log(date1.isSame(date2)); // false
var date1 = moment("2023-09-22"); var date2 = moment("2023-09-30"); var diffInDays = date2.diff(date1, 'days'); console.log(diffInDays); // 8 ngày
var date1 = moment("2022-01-01"); var date2 = moment("2024-09-22"); console.log(date2.diff(date1, 'years')); // 2 năm console.log(date2.diff(date1, 'months')); // 32 tháng
Moment.js cung cấp hỗ trợ cho Moment Timezone để quản lý thời gian ở các múi giờ khác nhau.
Bạn cần cài đặt Moment Timezone:
npm install moment-timezone
var newYorkTime = moment.tz("2024-09-22 14:30", "America/New_York"); console.log(newYorkTime.format()); // Hiển thị thời gian tại New York
Bạn có thể chuyển đổi giữa các múi giờ:
var londonTime = newYorkTime.clone().tz("Europe/London"); console.log(londonTime.format()); // Hiển thị thời gian tại London
var now = moment(); console.log(now.year()); // Lấy năm console.log(now.month()); // Lấy tháng (0-11) console.log(now.date()); // Lấy ngày
var now = moment(); now.year(2025).month(11).date(25); // Đặt thành 25/12/2025 console.log(now.format("YYYY-MM-DD"));
Lưu ý: Mặc dù Moment.js vẫn được sử dụng rộng rãi, bạn nên xem xét các giải pháp hiện đại hơn như Day.js hoặc các API thời gian gốc của JavaScript (Date
và Intl.DateTimeFormat
) vì chúng nhẹ hơn và hiệu quả hơn.
Nếu bạn đã sử dụng Moment.js cho dự án hiện tại, các thao tác trên sẽ giúp bạn làm việc hiệu quả với ngày tháng và thời gian.