Author: | dgtlss |
---|---|
Views Total: | 0 views |
Official Page: | Go to website |
Last Update: | November 10, 2024 |
License: | MIT |
Preview:

Description:
Chronos is a lightweight and fast date & time parsing, manipulating, and formatting JavaScript library ideal for web developers handling complex date-related tasks.
It can be useful for a variety of applications, like scheduling, calendars, data visualization, and even just displaying the current date and time on a website.
Key Features:
- Intuitive API: Simple and easy-to-use methods for common date/time operations.
- Parsing & Formatting: Flexible date parsing and customizable output formats.
- Time Zone Support: Handles different time zones accurately.
- Date Arithmetic: Easily add and subtract time units.
- Date Comparison: Compare dates with various methods.
- Utility Functions: Helpful methods for common date checks (weekdays, weekends, etc.).
- Advanced Features: Duration calculation, human-readable differences, business day calculations.
- Static Methods: Convenient methods for creating and parsing dates.
How to use it:
1. Download and load the minified version of the Chronos library in the document.
<script src="chronos.min.js"></script>
2. Create a date object using multiple formats:
// Current Date and Time: const now = new Chronos(); // From a Date String const date = new Chronos('2024-11-10T21:30:00Z'); // From a Timestamp const timestamp = new Chronos(1731294338); // With a Specific Time Zone const ukDate = new Chronos('2024-11-10T21:30:00Z', 'Europe/London');
3. Extract date information from a date object.
const date = new Chronos('2024-11-10T21:30:00Z'); // 2024 date.year(); // 11 date.month(); // 10 date.date(); // 1 = Monday // 0 = Sunday date.day(); // 21 date.hours(); // 30 date.minutes(); // 00 date.seconds(); // 00 date.milliseconds(); // 1731274200 date.timestamp();
4. Add or subtract time using add(value, unit)
and subtract(value, unit)
. Units include ‘years’, ‘months’, ‘weeks’, ‘days’, ‘hours’, ‘minutes’, and ‘seconds’.
const date = new Chronos('2024-11-10T21:30:00Z'); // 11-10-2024 date.add(1, 'day').format('MM-DD-YYYY')); // 11-10-2024 20:30 date.subtract(1, 'hours').format('MM-DD-YYYY HH:mm'));
5. Format the date with format(formatString). Tokens like YYYY, MM, DD, HH, mm, ss define the format. See the examples in Step 4.
6. Compare dates using methods like isBefore(other)
, isAfter(other)
, isSame(other)
, isBetween(start, end)
, and more granular checks like isSameDay(other)
. Returns true or false.
const date1 = new Chronos('2024-11-10T21:30:00Z'); const date2 = new Chronos('2024-11-11T21:30:00Z'); date1.isBefore(date2); date1.isAfter(date2); date1.isSame(date2); date1.isBetween(start, end); date1.isSameDay(date2); date1.isSameMonth(date2); date1.isSameYear(date2);
7. Check if a date is a weekday (isWeekday()
), weekend (isWeekend()
), today (isToday()
), in the future (isFuture()
), and other helpful functions. Returns true or false.
const date = new Chronos('2024-11-10T21:30:00Z'); date.isWeekday(); date.isWeekend(); date.isToday(); date.isTomorrow(); date.isYesterday(); date.isFuture(); date.isPast(); date.isLeapYear(); date.quarter();
8. Convert date objects into various formats.
const date = new Chronos('2024-11-10T21:30:00Z'); date.toDate(); // JS Date object date.valueOf(); // timestamp in milliseconds date.toString(); // ISO string date.toJSON(); date.toArray(); date.toObject();
9. Calculate differences between dates in a human-readable format, get durations, add business days, and work with start and end times of time units.
const date1 = new Chronos('2024-11-10T21:30:00Z'); const date2 = new Chronos('2024-11-11T21:30:00Z'); // 1 days ago date1.diffForHumans(date2, options) // { days: 1, hours: 0, minutes: 0, seconds: 0 } date1.duration(date2) date1.addBusinessDays(days); // Unit: year, month, day, hour date1.startOf(unit); date1.endOf(unit);
10. Chronos also offers static methods like Chronos.now()
, Chronos.parse()
, and others for creating and parsing dates directly from the Chronos class.
Chronos.now(timezone); Chronos.parse(input, timezone); Chronos.createFromFormat(format, dateString); Chronos.createFromTimestamp(timestamp); Chronos.createFromTimestampMs(timestampMs); Chronos.generateCalendar(year, month);