dater-lib
v1.0.0
Published
small and handy library for working with dates. It provides functions for formatting, getting the current date, and calculating the difference between dates.
Readme
dater-lib
dater-lib is tiny lightweight library for working with dates, providing functions for formatting, getting the current date, and calculating the difference between dates.
Installation
Install the library via npm:
npm install dater-libUsage
Import the functions from the library and use them in your project:
const { formatDate, now, formattedNow, daysBetween, getDayOfWeekName } = require('dater-lib');
// Example usage
const currentDate = new Date();
console.log(formatDate(currentDate, "yyyy-MM-dd hh:mm:ss")); // "2024-12-23 15:30:45"
console.log(now()); // Current time in milliseconds
console.log(formattedNow()); // Current date in "yyyy-MM-dd hh:mm:ss" format
console.log(daysBetween(new Date("2024-01-01"), new Date("2023-01-01"))); // Difference in days
console.log(getDayOfWeekName(currentDate)); // "Monday"API
formatDate(date, formatString="yyyy-MM-dd hh:mm:ss")
Formats a date according to the specified template.
- Parameters:
date(Date): The date to format.formatString(string): The formatting template. Supported placeholders:yyyy— yearMM— monthdd— day of the monthddd— day of the week namehh— hoursmm— minutesss— seconds
- Returns: A string with the formatted date.
now()
Returns the current time in milliseconds since January 1, 1970.
- Returns: A number.
formattedNow()
Returns the current date formatted as yyyy-MM-dd hh:mm:ss.
- Returns: A string.
daysBetween(date1, date2)
Calculates the difference in days between two dates.
- Parameters:
date1(Date): The first date.date2(Date): The second date.
- Returns: The difference in days (number). If the second date is later than the first, the result will be negative.
getDayOfWeekName(date)
Returns the name of the day of the week for a given date.
- Parameters:
date(Date): The date for which to get the day of the week name.
- Returns: A string with the name of the day of the week (e.g., "Monday").
Example
const { formatDate, formattedNow, daysBetween, getDayOfWeekName } = require('dater-lib');
console.log(formattedNow()); // "2024-12-23 15:30:45"
console.log(daysBetween(new Date("2024-01-01"), new Date("2023-01-01"))); // 365
console.log(getDayOfWeekName(new Date())); // "Monday"License
MIT
