@craftykate/kates-dates
v1.3.0
Published
Kate's date package
Downloads
3
Readme
Kate's Dates
Lightweight helper functions for date objects in Javascript
Introduction
I can never remember how to figure out how many days in the month there are, or how to get the day of the week, or how to get the day of the month but pad it with zeros. So I made a lightweight package to do these for me that makes intuitive sense to me.
(This is a brand new package and I intend to add more functionality as I need it or think of it!)
Installation
- Run
npm i @craftykate/kates-dates
in your consuming app to install
Usage
- In the consuming file, import with
import { KatesDates } from '@craftykate/kates-dates'
new KatesDates()
takes three parameters:- Param 1 (required): Date object. (ex:
new Date()
) - Param 2 (optional): Number - how many days away from the date you passed in. (ex:
-3
would be three days earlier,1
would be the day after) - Param 3 (optional): boolean - whether to set the time to 00:00:00
- Param 1 (required): Date object. (ex:
- Set up a new instance of the class with:
- Today:
const d = new KatesDates(new Date())
- Yesterday:
const d = new KatesDates(new Date(), -1)
- Tomorrow:
const d = new KatesDates(new Date(), +1)
- Today at midnight:
const d = new KatesDates(new Date(), 0, true)
- Today:
Available functions
- Year
d.year(numDigits?: 2 | 4)
: yyyy (default) or yy- Examples:
d.year()
ord.year(4)
// 2023d.year(2)
// 23
- Month in Letters
d.monthL(numLetters?: 1 | 3)
: January (default) or Jan or J- Examples:
d.monthL()
// Januaryd.monthL(3)
// Jand.monthL(1)
// J
- Month in Digits
d.monthD(minDigits?: 1 | 2))
: 1-12 (default) or 01-12- Examples:
d.monthD()
ord.monthD(1)
// 1, 14 (no padding)d.monthD(2)
// 01, 14 (always two digits)
- Month index
d.monthI()
: 0-11
- Month array
d.monthArray()
: ['January', 'February'...]
- Weekday in letters
d.weekdayL(numLetters?: 1 | 2 | 3)
: Sunday (default) or Sun or Su or S- Examples:
d.weekdayL()
// Sundayd.weekdayL(3)
// Sund.weekdayL(2)
// Sud.weekdayL(1)
// S
- Weekday index
d.weekdayI()
: // 0-6, 0 = Sunday
- Weekday array
d.weekdayArray()
: // ['Sunday', 'Monday', ...]
- Date of the month
d.date(minDigits?: 1 | 2, isSuffix = false)
: 1-31 (default) or 01-31 or 1st-31st- Examples:
d.date()
ord.date(1)
// 1, 14 (no padding)d.date(2)
// 01, 14 (always two digits)d.date(1, true)
// 1st, 14th, 22nd (adds 'st', 'nd', 'rd' or 'th')
- Time
- Hour
d.hour(numDigits?: 1 | 2, isMilitary = false)
: 1-12 (default) or 01-12 or 0-23 or 00-23- Examples:
d.hour()
ord.hour(1)
// 1, 12 (no padding, up to 12)d.hour(2)
// 01, 12 (two digits, up to 12)d.hour(1, true)
// 0, 3, 23 (no padding, 0-23)d.hour(2, true)
// 00, 03, 23 (two digits, 00-23)
- Minute
d.minute()
: 00-59 (always two digits)
- Second
d.second()
: 00-59 (always two digits)
- am / pm
d.ampm(numLetters?: 1 | 2, uppercase = false)
: am/pm (default) or a/p or AM/PM or A/P- Examples:
d.ampm()
ord.ampm(2)
// am, pmd.ampm(1)
// a, pd.ampm(2, true)
// AM, PMd.ampm(1, true)
// A, P
- Hour
- Number of days in month
d.daysInMonth()
: 28 or 29 or 30 or 31
- Days between
d.daysBetween(secondDate: Date)
: positive or negative number- Example:
d.daysBetween(new Date('01/01/2024'))
// 199 - means 1/1/2024 is 199 days in the future as I'm writing thisd.daysBetween(new Date('01/01/2023'))
// -166 - 1/1/2023 is 166 days in the past
- Get date string for form date fields
d.fieldFormat()
: yyyy-mm-dd
- Get the entire date object
d.dateObj()
: Date object
- Help
- Log out all functions available with examples using the date you have stored
d.help()
:
Examples:
const d = new KatesDates(new Date())
const wordyDate = `${d.weekdayL(3)}, ${d.monthL(3)} ${d.date(1, true)}, ${d.year()}`
console.log(wordyDate) // Fri, Jun 16th, 2023
const numberyDate = `${d.monthD(2)}/${d.date(2)}/${d.year()}`
console.log(numberyDate) // 06/16/2023
const justTheTime = `${d.hour()}:${d.minute()}${d.ampm(1)}`
console.log(justTheTime) // 6:43p