0.1.2 • Published 2 years ago

udx-calendara v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

UDX-Calendara

udx-calendara is a lightweight TypeScript library for formatting dates. It provides three main methods: formatDate, addPeriodTo, and subPeriodTo.

The formatDate method is highly inspired by useFormatDate from VueUse

Installation

npm install udx-calendara

Usage

The library offers three main methods for formatting dates: formatDate, addPeriodTo, and subPeriodTo.

formatDate(options: FormatOptions): string

Formats a date according to the specified format string:

FormatOutputDescription
YY18Two-digit year
YYYY2018Four-digit year
M1-12The month, beginning at 1
MM01-12The month, 2-digits
MMMJan-DecThe abbreviated month name
MMMMJanuary-DecemberThe full month name
D1-31The day of the month
DD01-31The day of the month, 2-digits
H0-23The hour
HH00-23The hour, 2-digits
h1-12The hour, 12-hour clock
hh01-12The hour, 12-hour clock, 2-digits
m0-59The minute
mm00-59The minute, 2-digits
s0-59The second
ss00-59The second, 2-digits
SSS000-999The millisecond, 3-digits
AAM PMThe meridiem
AAA.M. P.M.The meridiem, periods
aam pmThe meridiem, lowercase
aaa.m. p.m.The meridiem, lowercase and periods
d0-6The day of the week, with Sunday as 0
ddS-SThe min name of the day of the week
dddSun-SatThe short name of the day of the week
ddddSunday-SaturdayThe name of the day of the week

Example:

import { formatDate } from 'udx-calendara'

const date = new Date('2023-10-27T14:38:33.000Z')
const formattedDate = formatDate({ date, formatStr: 'YYYY-MM-DD' })
console.log(formattedDate) // Output: '2023-10-27'

addPeriodTo(options: PeriodOptions): string

Adds a specified number of days, months, or years to a date.

Period to add:

type Period = 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years'

Example:

import { addPeriodTo } from 'udx-calendara'

const date = new Date('2023-10-27T14:38:33')
const formattedDate = addPeriodTo({ date, n: 2, period: 'days', formatStr: 'YYYY-MM-DD' })
console.log(formattedDate) // Output: '2023-10-29'

subPeriodTo(options: PeriodOptions): string

Subtracts a specified number of days, months, or years from a date.

Period to add:

type Period = 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years'

Example:

import { subPeriodTo } from 'udx-calendara'

const date = new Date('2023-10-27T14:38:33')
const formattedDate = subPeriodTo({ date, n: 2, period: 'days', formatStr: 'YYYY-MM-DD' })
console.log(formattedDate) // Output: '2023-10-25'

Test Examples

Here are some test examples demonstrating how to use the library's methods:

it('should remove 2 days to a date', () => {
    // ...
})

it('should add 5 months to a date', () => {
    // ...
})

it('should format a date in a custom format', () => {
    // ...
})

Contributing

We welcome contributions! If you want to contribute to this library, follow these steps:

  1. Fork the repository
  2. Create a branch with your feature (git checkout -b feature/feature-name)
  3. Commit your changes (git commit -m 'Add a new feature')
  4. Push your branch (git push origin feature/feature-name)
  5. Open a Pull Request

License

This project is licensed under the MIT License. See the LICENSE file for more details.

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago