Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
date-and-time
Advanced tools
The date-and-time npm package is a lightweight library for date and time manipulation in JavaScript. It provides a variety of functions for parsing, formatting, adding, subtracting, and comparing dates and times.
Formatting Dates
This feature allows you to format dates into various string representations. The format method takes a date object and a format string as arguments.
const date = new Date(2023, 9, 10);
const dateAndTime = require('date-and-time');
const formattedDate = dateAndTime.format(date, 'YYYY/MM/DD HH:mm:ss');
console.log(formattedDate); // Output: 2023/10/10 00:00:00
Parsing Dates
This feature allows you to parse date strings into JavaScript Date objects. The parse method takes a date string and a format string as arguments.
const dateAndTime = require('date-and-time');
const dateString = '2023/10/10 00:00:00';
const parsedDate = dateAndTime.parse(dateString, 'YYYY/MM/DD HH:mm:ss');
console.log(parsedDate); // Output: Tue Oct 10 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
Adding and Subtracting Time
This feature allows you to add or subtract time units (days, months, years, etc.) from a date. The addDays and addMonths methods are used in this example.
const dateAndTime = require('date-and-time');
let date = new Date(2023, 9, 10);
date = dateAndTime.addDays(date, 5);
console.log(date); // Output: Sun Oct 15 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
date = dateAndTime.addMonths(date, -1);
console.log(date); // Output: Fri Sep 15 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
Comparing Dates
This feature allows you to compare dates. The isSameDay method checks if two dates fall on the same day.
const dateAndTime = require('date-and-time');
const date1 = new Date(2023, 9, 10);
const date2 = new Date(2023, 9, 15);
const isSameDay = dateAndTime.isSameDay(date1, date2);
console.log(isSameDay); // Output: false
Moment.js is a widely-used library for date and time manipulation. It offers extensive functionality for parsing, validating, manipulating, and formatting dates. Compared to date-and-time, Moment.js is more feature-rich but also larger in size.
date-fns is a modern JavaScript date utility library that provides a comprehensive set of functions for date manipulation. It is modular, allowing you to import only the functions you need, making it more lightweight compared to Moment.js. date-fns is similar to date-and-time in terms of functionality but offers a more functional programming approach.
Luxon is a modern library for working with dates and times in JavaScript. It is built by one of the Moment.js developers and offers a more modern API with better support for internationalization. Luxon is more feature-rich than date-and-time and is designed to be a more modern alternative to Moment.js.
Date-and-time is date and time utilities for Node.js and a browser.
Probably the most famous date library is Moment.js. It is so great, but has been bloated (11.3k gz). It needs more simple one.
Node.js:
$ npm install date-and-time --save
a browser:
<script src="./src/date-and-time.min.js"></script>
via require():
var date = require('date-and-time');
via window or self object:
var date = window.date;
var now = new Date();
date.format(now, 'YYYY/MM/DD HH:mm:ss'); // '2015/01/02 23:14:05'
date.format(now, 'E MMM DD YYYY'); // 'Fri Jan 02 2015'
formatString | meaning | examples |
---|---|---|
YYYY | year | 2015, 1999 |
YY | year | 15, 99 |
MMM | month | Jan, Dec |
MM | month | 01, 12 |
M | month | 1, 12 |
DD | day | 02, 31 |
D | day | 2, 31 |
E | day of week | Fri, Sun |
HH | hour-24 | 23, 08 |
H | hour-24 | 23, 8 |
A | meridian | p.m., a.m. |
hh | hour-12 | 11, 08 |
h | hour-12 | 11, 8 |
mm | minute | 14, 07 |
m | minute | 14, 7 |
ss | second | 05, 10 |
s | second | 5, 10 |
SSS | millisecond | 753, 022 |
SS | millisecond | 75, 02 |
S | millisecond | 7, 0 |
date.parse('2015/01/02 23:14:05', 'YYYY/MM/DD HH:mm:ss'); // Date object
date.parse('02-01-2015', 'DD-MM-YYYY'); // Date object
formatString | meaning | examples |
---|---|---|
YYYY | year | 2015, 1999 |
YY | year | 15, 99 |
MM | month | 01, 12 |
M | month | 1, 12 |
DD | day | 02, 31 |
D | day | 2, 31 |
HH | hour-24 | 23, 08 |
H | hour-24 | 23, 8 |
mm | minute | 14, 07 |
m | minute | 14, 7 |
ss | second | 05, 10 |
s | second | 5, 10 |
SSS | millisecond | 753, 022 |
SS | millisecond | 75, 02 |
S | millisecond | 7, 0 |
var now = new Date();
var next_year = date.addYears(now, 1); // Date object
var now = new Date();
var next_month = date.addMonths(now, 1); // Date object
var now = new Date();
var yesterday = date.addDays(now, -1); // Date object
var now = new Date();
var an_hour_ago = date.addHours(now, -1); // Date object
var now = new Date();
var two_minutes_later = date.addMinutes(now, 2); // Date object
var now = new Date();
var three_seconds_ago = date.addSeconds(now, -3); // Date object
var now = new Date();
var a_millisecond_later = date.addMilliseconds(now, 1); // Date object
var today = new Date(2015, 0, 2);
var yesterday = new Date(2015, 0, 1);
date.subtract(today, yesterday).toDays(); // 1
date.subtract(today, yesterday).toHours(); // 24
date.subtract(today, yesterday).toMinutes(); // 1440
date.subtract(today, yesterday).toSeconds(); // 86400
date.subtract(today, yesterday).toMilliseconds(); // 86400000
date.parse('2015/01/02 23:14:05', 'YYYY/MM/DD HH:mm:ss'); // true
date.parse('02-29-2015', 'DD-MM-YYYY'); // false
formatString
is the same as one of parse
.
var date1 = new Date(2015, 0, 2);
var date2 = new Date(2012, 0, 2);
date.isLeapYear(date1); // false
date.isLeapYear(date2); // true
Months, day of week, and meridian are written in English. If you want to change language, rewrite them.
var date = require('date-and-time');
// French
date.month = ['janv.', 'févr.', 'mars', 'avr.', 'mai', 'juin', 'juil.', 'août', 'sept.', 'oct.', 'nov.', 'déc.'];
date.dayOfWeek = ['dim.', 'lun.', 'mar.', 'mer.', 'jeu.', 'ven.', 'sam.'];
date.format(new Date(), 'E MMM DD YYYY'); // ven. janv. 02 2015
// Japanese
date.dayOfWeek = ['日', '月', '火', '水', '木', '金', '土'];
date.hour12.meridian = ['午前', '午後'];
date.format(new Date(), 'YYYY年MM月DD日(E) Ahh時'); // 2015年01月02日(金) 午前10時
日本では時刻を午前と午後に分ける12時間制の表現が英米式と異なるばかりでなく、それ自体に曖昧さがあり、しばしば混乱が生じます。
そのため日本のシステム開発の現場では多くの場合24時間制が用いられるかと思いますが、念のため12時間制を用いる必要が生じた場合の対策方法について説明します。
まず、日本での12時間制についてはWikipediaのこちらのページで詳しく解説されています。
date-and-time
では英米式の12時間制をデフォルト実装しているため、日本向けにロジックのカスタマイズが必要です。以下に例をご紹介します。
var date = require('date-and-time');
// 時刻を12時間制に変換するロジックの書き換え
date.hour12.h = function (d) {
var h = d.getHours();
return h > 11 ? h - 12 : h;
};
// 午前/午後を返すロジックの書き換え
date.hour12.A = function (d) {
return this.meridian[d.getHours() > 11 | 0];
};
上記の例はWikipediaで説明されている「日本時計協会」方式です。「日本式」はロジックがより複雑になるため割愛させていただきます。
Chrome, Android, Firefox, Safari, Mobile Safari, Opera, and Internet Explorer 6+.
MIT
FAQs
A Minimalist DateTime utility for Node.js and the browser
The npm package date-and-time receives a total of 409,329 weekly downloads. As such, date-and-time popularity was classified as popular.
We found that date-and-time demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.