1.0.3 • Published 4 years ago

wizardate v1.0.3

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

Wizardate

NPM Version license npm monthly npm downloads for wizardate

Wizardate provides a set of date formats for different locations. It allows you to create and register your own date formats and patterns.

Table of content


Usage

import Wizardate, { pt_BR } from 'wizardate';

const wizardate = new Wizardate(pt_BR);
wizardate.getDate(); // => 17/07/2020

Locales and Formats

Date Formats

  • LittleEndian - dd/mm/yyyy or 22 April 2006

  • MiddleEndian - mm/dd/yyyy or April 22 2006

  • BigEndian - yyyy/mm/dd or 2006 April 22

Locales

All locales has your own default configuration

  • pt_BR
    • LittleEndian format
    • date separator /
    • time separator :
  • en
    • MiddleEndian format
    • date separator -
    • time separator :
  • fr
    • LittleEndian format
    • date separator /
    • time separator :
  • es
    • LittleEndian format
    • date separator /
    • time separator :
  • de
    • LittleEndian format
    • date separator /
    • time separator :
  • it
    • LittleEndian format
    • date separator /
    • time separator :

Config Functions

config(object)

Used to separate date and time

  • {string} sepDate - used to separate date

    • e.g.: 02 / 11 / 2019
  • {string} sepTime - used to separate time

    • e.g.: 08 : 53 : 20
import Wizardate, { fr } from 'wizardate';

const wizardate = new Wizardate(fr);
wizardate.config({
    sepDate: '-',
    sepTime: ':'
})

setDefaultConfig()

Set default config according to locale

import Wizardate, { en } from 'wizardate';

const wizardate = new Wizardate(en);
wizardate.setDefaultConfig()

setDateFormat(dateFormat)

import Wizardate, { pt_BR, BigEndian } from 'wizardate';

const wizardate = new Wizardate(pt_BR);
wizardate.setDateFormat(BigEndian);

Registry Function

Wizardate allows you to create and register your own functions. Registered functions are stored in the funcs props

registry(funcName, func)

  • {string} funcName - function name
  • {function} func - function callback
import Wizardate, { pt_BR, en, BigEndian } from 'wizardate';

const wizardateBR = new Wizardate(pt_BR);

wizardateBR.registry('myDate', () => {
  return `Today is ${wizardateBR.getWeekday()}, ${wizardateBR.getDate()}`;
});

console.log(wizardateBR.funcs.myDate()); // => Today is friday, 17/07/2020

const wizardateEN = new Wizardate(en);
wizardateEN.setDateFormat(BigEndian);

// Using Format Pattern
wizardateEN.registry('countryCodeAndDate', () => {
  return wizardateEN.formatPattern("'countryCode', 'date'");
});

console.log(wizardateEN.funcs.countryCodeAndDate()); // => BR, 2020-07-17

Date Functions

All functions below receive an optional date as a parameter. If no date is passed the function uses the current date.

  • {string} date - accepts various formats (optional)

e.g.:

wizardate.getDateTime('2012-01-26T13:51:50.417-07:00') // => 26/01/2012 18:51:50

wizardate.getTime('05/07/2019 08:54:32') // => 08:54:32

wizardate.getDate('October 15, 1996 05: 35: 32') // => 15/10/1996

wizardate.getExtendedWeekdayDate('2017-09-08T15:25:53Z') // => monday, 08 september 2017

wizardate.getMonthNumber('05 October 2011 14:48 UTC') // => 10

wizardate.getDateTime('07/24/2015') // => 24/07/2018 00:00:00

wizardate.getWeekday('05 October 2011 14:48 UTC') // => friday

wizardate.getExtendedTime('Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)' // => 11 hours 48 minutes 00 seconds

wizardate.getShortMonthYear('2011-10-05T14:48:00.000Z') // => out/2011

wizardate.getDay('24HJERH87LK4') // => Invalid Date

If the date passed is invalid the functions will return 'Invalid Date'.

getDay(date)

  • returns day (1 ~ 31)
wizardate.getDay() // => 17

getMonthNumber(date)

  • returns month number (1 ~ 12)
wizardate.getMonthNumber() // => 7

getMonth(date)

wizardate.getMonth() // => july
numbermonth
01january
02february
03march
04april
05may
06june
07july
08august
09september
10october
11november
12december

getShortMonth(date)

wizardate.getShortMonth() // => jul
numbermonth
01jan
02feb
03mar
04apr
05may
06jun
07jul
08aug
09sep
10oct
11nov
12dec

getYear(date)

wizardate.getYear() // => 2020

getShortYear(date)

wizardate.getShortYear('07/18/2019') // => 20

getWeekdayNumber(date)

  • returns the number of the week (1 a 7)
wizardate.getWeekdayNumber() // => 5

getWeekday(date)

wizardate.getWeekday() // => friday
numberweekday
1sunday
2monday
3tuesday
4wednesday
5thursday
6friday
7saturday

getShortWeekday(date)

wizardate.getShortWeekday() // => fri
numberweekday
1sun
2mon
3tue
4wed
5thu
6fri
7sat

getDate(date)

wizardate.getDate() // => 17/07/2020

getExtendedDate(date)

wizardate.getExtendedDate() // => 17 july 2020

getExtendedWeekdayDate(date)

wizardate.getExtendedWeekdayDate() // => friday, 17 july 2020

getMonthNumberYear(date)

wizardate.getMonthNumberYear() // => 07/2020

getMonthYear(date)

wizardate.getMonthYear() // => july/2020

getShortMonthYear(date)

wizardate.getShortMonthYear() // => jul/2020

getShortMonthShortYear(date)

wizardate.getShortMonthYear() // => jul/20

getHour(date)

  • return hours (1 a 23)
wizardate.getHour() // => 8

getExtendedHour(date)

wizardate.getExtendedHour() // => 08 hours

getMinute(date)

  • return minutes (1 a 59)
wizardate.getMinute() // => 53

getExtendedMinute(date)

wizardate.getExtendedMinute() // => 53 minutes

getSecond(date)

  • returns seconds (1 a 59)
wizardate.getSecond() // => 32

getExtendedSecond(date)

wizardate.getExtendedSecond() // => 32 seconds

getTime(date)

wizardate.getTime() // => 08:53:32

getExtendedTime(date)

wizardate.getExtendedTime() // => 08 hours 53 minutes 32 seconds

getHourMinute(date)

wizardate.getHourMinute() // => 08:53

getExtendedHourMinute(date)

wizardate.getExtendedHourMinute() // => 08 hours 53 minutes

getDateTime(date)

wizardate.getDateTime() // => 17/06/2020 08:53:32

getExtendedDateTime(date)

wizardate.getExtendedDateTime() // => 17 july 2020 08 hours 53 minutes 32 seconds

getExtendedCityDate(date)

asynchronous function

await wizardate.getExtendedCityDate() // =>  Marília, 17 july 2020

getExtendedRegionDate(date)

asynchronous function

await wizardate.getExtendedRegionDate() // =>  São Paulo, 17 july 2020

getExtendedRegionCodeDate(date)

asynchronous function

await wizardate.getExtendedRegionCodeDate() // =>  SP, 17 july 2020

getExtendedCountryDate(date)

asynchronous function

await wizardate.getExtendedCountryDate() // =>  Brazil, 17 july 2020

getExtendedCountryCodeDate(date)

asynchronous function

await wizardate.getExtendedCountryCodeDate() // =>  BR, 17 july 2020

Current Locale Functions

All functions bellow are asynchronous and returns current locale informations.

getCity()

asynchronous function

await wizardate.getCity() // => Marília

getRegion()

asynchronous function

await wizardate.getRegion() // => São Paulo

getRegionCode()

asynchronous function

await wizardate.getRegionCode() // => SP

getCountry()

asynchronous function

await wizardate.getCountry() // => Brazil

getCountryCode()

asynchronous function

await wizardate.getCountryCode() // => BR

getTimezone()

asynchronous function

await wizardate.getTimezone() // => America/Sao_Paulo

getZipcode()

asynchronous function

await wizardate.getZipcode() // => 17350-000

Format Pattern

Use patterns to create your date format

patternoutputdesc
'date'17/07/2020full date
'time'16:09:30full time
'd'3day withou zero
'dd'03day with zero
'm'7month without zero
'mm'07month with zero
'mmm'julshort month
'mmmm'julymonth
'MMM'Julcapitalize short month
'MMMM'Julycapitalize month
'yy'20short year
'yyyy'2020year
'w'6weekday withou zero
'ww'06weekday with zero
'www'frishort weekday
'wwww'fridayweekday
'WWW'Fricapitalize short weekday
'WWWW'Fridaycapitalize weekday
'h'5hour without zero
'hh'05hour with zero
'hhh'5 hourhour without zero and desc
'hhhh'05 hourhour with zero and desc
'HHH'5 Hourhour without zero and capitalize desc
'HHHH'05 Hourhour with zero and capitalize desc
'i'7minute without zero
'ii'07minute with zero
'iii'7 minutesminute without zero and desc
'iiii'07 minutesminute with zero and desc
'III'7 Minutesminute without zero and capitalize desc
'IIII'07 Minutesminute with zero and capitalize desc
's'1second without zero
'ss'01second with zero
'sss'1 secondsecond without zero and desc
'ssss'01 secondsecond with zero and desc
'SSS'1 Secondsecond without zero and capitalize desc
'SSSS'01 Secondsecond with zero and capitalize desc
'city'Maríliacity
'region'Sao Pauloregion
'regionCode'SPregion code
'country'Brazilcountry
'countryCode'BRcountry code

Formats available only for pt_BR

patternoutputdesc
'wwwww'sexta-feiraweekday
'WWWWW'Sexta-feiracapitalize weekday

formatPattern(pattern, date)

  • {string} pattern - string patter
  • {string} date - accepts various formats (optional)
const myPattern = "'MMM' 'yyyy' - My Pattern"

wizardate.formatPattern(myPattern); // => Jul 2020 - My Pattern

Changelog

see the update notes at CHANGELOG.

License

MIT License

Copyright ® 2020 Victor Gianvechio

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.