lazyidli_date-time
v1.0.3
Published
A simple and easy-to-use TypeScript/Javascript date and time library with convenient keywords for dates, timestamps, and more.
Downloads
7
Readme
README.md
# Lazy Idli Date and Time TS
`lazyidli_time-date` is a simple, user-friendly, and lightweight TypeScript library that provides convenient utility<br/>functions for handling dates, times, UNIX timestamps, and more. It also supports easy-to-use keywords like `getToday`,<br/>`getTomorrow`, `getCurrentTime`, and includes features like age calculation, date conversions, and adding or <br/>subtracting time units.
## Features
- Get today's date, tomorrow's date, and current time with simple functions.
- Convert to and from UNIX timestamps.
- Calculate age based on date of birth.
- Add or subtract days, months, and other time units from any date.
- Get current day of the week, current month, and current week number.
- Fully written in TypeScript for type safety and modern development.
## Installation
You can install the package via npm:
```bash
npm install lazyidli_time-date
Requirements
- Node.js: Version >=12.0
- TypeScript: Recommended for better type safety (though the library also works with JavaScript).
Usage
Importing the Library
import {
getToday,
getTomorrow,
getCurrentTime,
getCurrentWeek,
toUnixTimestamp,
fromUnixTimestamp,
calculateAge,
addDays,
addMonths
} from 'lazyidli_time-date';
Examples
1. Get Today’s Date
const today = getToday();
console.log(today); // Output: '2024-08-25'
2. Get Tomorrow’s Date
const tomorrow = getTomorrow();
console.log(tomorrow); // Output: '2024-08-26'
3. Get Current Time
const time = getCurrentTime();
console.log(time); // Output: '14:45:30'
4. Convert to UNIX Timestamp
const unixTimestamp = toUnixTimestamp('2024-08-25');
console.log(unixTimestamp); // Output: 1724380800
5. Convert from UNIX Timestamp
const readableDate = fromUnixTimestamp(1724380800);
console.log(readableDate); // Output: '2024-08-25 00:00:00'
6. Calculate Age from Date of Birth
const age = calculateAge('1990-05-15');
console.log(age); // Output: 34
7. Add Days to a Date
const newDate = addDays('2024-08-25', 10);
console.log(newDate); // Output: '2024-09-04'
8. Add Months to a Date
const newDate = addMonths('2024-08-25', 2);
console.log(newDate); // Output: '2024-10-25'
9. Get Current Week of the Year
const week = getCurrentWeek();
console.log(week); // Output: 34
Full API Reference
getToday()
Description: Returns today's date in
YYYY-MM-DD
format.Example:
const today = getToday(); // '2024-08-25'
getTomorrow()
Description: Returns tomorrow's date in
YYYY-MM-DD
format.Example:
const tomorrow = getTomorrow(); // '2024-08-26'
getCurrentTime()
Description: Returns the current time in
HH:mm:ss
format.Example:
const time = getCurrentTime(); // '14:45:30'
getCurrentWeek()
Description: Returns the current week number of the year.
Example:
const week = getCurrentWeek(); // 34
toUnixTimestamp(dateStr: string, format: string = 'YYYY-MM-DD')
Description: Converts a date string to a UNIX timestamp.
Parameters:
dateStr
: The date string to convert (e.g.'2024-08-25'
).format
: Optional format of the date string. Default is'YYYY-MM-DD'
.
Example:
const unixTimestamp = toUnixTimestamp('2024-08-25'); // 1724380800
fromUnixTimestamp(unixTime: number, format: string = 'YYYY-MM-DD HH:mm:ss')
Description: Converts a UNIX timestamp to a human-readable date string.
Parameters:
unixTime
: The UNIX timestamp to convert (e.g.1724380800
).format
: Optional format for the output date. Default is'YYYY-MM-DD HH:mm:ss'
.
Example:
const date = fromUnixTimestamp(1724380800); // '2024-08-25 00:00:00'
calculateAge(dob: string, format: string = 'YYYY-MM-DD')
Description: Calculates age based on the date of birth.
Parameters:
dob
: The date of birth as a string (e.g.'1990-05-15'
).format
: Optional format of the date string. Default is'YYYY-MM-DD'
.
Example:
const age = calculateAge('1990-05-15'); // 34
addDays(dateStr: string, days: number, format: string = 'YYYY-MM-DD')
Description: Adds a specified number of days to a given date.
Parameters:
dateStr
: The date to which days should be added.days
: The number of days to add.format
: Optional format of the date string. Default is'YYYY-MM-DD'
.
Example:
const newDate = addDays('2024-08-25', 10); // '2024-09-04'
addMonths(dateStr: string, months: number, format: string = 'YYYY-MM-DD')
Description: Adds a specified number of months to a given date.
Parameters:
dateStr
: The date to which months should be added.months
: The number of months to add.format
: Optional format of the date string. Default is'YYYY-MM-DD'
.
Example:
const newDate = addMonths('2024-08-25', 2); // '2024-10-25'
License
- This project is licensed under the MIT License. See the LICENSE file for details.