better-date-functions
v1.1.3
Published
A JavaScript library with more functions for the normal Date class.
Downloads
18
Maintainers
Readme
Installation
BDF requires Node.js v10+ to run.
Documentation
Getting the BDF dependency
// import the the BDF dependency
import BDF from 'better-date-functions';
const {
getMonthLength,
getMonthName,
getWeekDayName,
getWeekDayNameWithADate,
setLanguage,
getCurrentLanguage,
getMonthInfo,
getYearLength,
setThrowingErrors,
getCurrentThrowingErrorsState,
} = BDF; // Destructuring (optional)
// Using the functions
// Days must be a number between 1~31
// Months must be a number between 1~12
// Year Must be a number
//Example:
const day = 24
const month = 1
const year = 2022
const weekDayName = getWeekDayNameWithADate(day, month, year);
console.log(weekDayName) // returns "Monday"
All examples
//Example 1:
const weekDay = 1
const weekDayName = getWeekDayName(weekDay)
console.log(weekDayName) // returns "Sunday"
//Example 2:
const month = 3
const monthName = getMonthName(month)
console.log(monthName) // returns "March"
//Example 3:
const month = 3
const monthName = getMonthName(month)
console.log(monthName) // returns "March"
//Example 4:
const month = 2
const year = 2024
const monthLength = getMonthLength(month, year) // Year is optional, but for precise Length in February you need the year
console.log(monthLength) // returns 29
//Example 5:
const month = 1
const year = 2022
const monthInfo = getMonthInfo(month, year) // Year is optional, but for precise Length in February you need the year
console.log(monthInfo) // returns { length: 31, name: "January", year: 2022 }
//Example 6:
const year1 = 2022
const year2 = 2024
const year1Length = getYearLength(year1)
const year2Length = getYearLength(year2)
console.log([year1Length, year2Length]) // returns [365, 366]
Examples with date
import { withDate } from 'better-date-functions' // import the withDate functions
// you can destructurate too
// const { withDate } = BDF;
//Examples:
const date = new Date() // currently on 26th January, 2022
const weekDayName = withDate.getWeekDayName(date)
console.log(weekDayName) // returns "Wednesday"
const monthLength = withDate.getMonthLength(date)
console.log(monthLength) // returns 31
const monthName = withDate.getMonthName(date)
console.log(monthName) // returns "January"
const monthInfo = withDate.getMonthInfo(date)
console.log(monthInfo) // returns { name: "January", length: 31, year: 2022 }
const yearLength = withDate.getYearLength(date)
console.log(yearLength) // returns 365
const formatedDate = withDate.getformatedDate(date)
console.log(formatedDate) // returns 01/26/2022
const separator = "-"
const formatedDateWithCustomSeparator = withDate.getformatedDate(date, separator)
console.log(formatedDateWithCustomSeparator) // returns 01-26-2022
Configurations
//Changing the language of the returns
const weekDay = 1
const date = new Date() // currently on 26th January, 2022
let weekDayName = getWeekDayName(weekDay)
console.log(weekDayName) // returns "Sunday"
let formatedDate = withDate.getformatedDate(date)
console.log(formatedDate) // returns 01/26/2022
setLanguage('ptBR') // Currently supports "enUs" and "ptBr"
weekDayName = getWeekDayName(weekDay)
console.log(weekDayName) // returns "Domingo"
formatedDate = withDate.getformatedDate(date)
console.log(formatedDate) // returns 26/01/2022
const currentLanguage = getCurrentLanguage() // to get the current language configurated
console.log(currentLanguage) // returns "ptBr"
//Change the throwing errors configuration
let isThrowingErrors = getCurrentThrowingErrorsState() // by default the value of the state is true
console.log(isThrowingErrors) // returns true
setThrowingErrors(false) // you can change the state using this function
isThrowingErrors = getCurrentThrowingErrorsState()
console.log(isThrowingErrors) // returns false