1.4.0 • Published 3 years ago

prime-time v1.4.0

Weekly downloads
42
License
SEE LICENSE IN LI...
Repository
github
Last release
3 years ago

MIT License Coverage Status

Prime Time

A lightweight JavaScript date library for parsing, manipulating and formatting dates.

Installing

The library is available as a npm package. To install the package simply run:

npm install prime-time

Calling the functionality

Basically all you need to create the object is call primetime(). This will result in a PrimeTime object containing the current time. You can also provide a timestamp, a string representation or a Date to the function call. This will return a PrimeTime object specified to that date. For example: primetime(519998462003) equals the date 1986-6-24 12:01:02.003 GMT.

Formatting dates

CodeDescriptionReturns
primetime().localise()Format the date to the user's locale (en-US for example)6/24/1986
primetime().localise('weekday, month, day, hour, minute, second, timezone')Format the date to the provided date parts and user's locale (en-US for example)Tuesday, 6/24/1986, 12:01:02 PM Coordinated Universal Time
primetime().localise('WDD, MMMM, DD, HH, mm, ss, TZZ', 'nl-nl')Format the date to the provided date parts and localedinsdag 24-6-1986 12:01:02 Gecoördineerde wereldtijd
primetime().customise('{weekday}, {month} {day}, {year} @ {hour}:{minute}:{second} {timezone-short}')Format the date to the provided format and user's locale (en-US for example)Tuesday, June 24, 1986 @ 12:01:02 UTC
primetime().customise('{WDD}, {MMMM} {DD}, {YY} @ {HH}:{mm}:{ss} {TZ}', 'nl-nl')Format the date to the provided format and user's localedinsdag, juni 24, 1986 @ 12:01:02 UTC
primetime().format()Format the date to the user's locale (en-US for example)6/24/1986
primetime().format('weekday, month, day, hour, minute, second, timezone')Format the date to the provided date parts and user's locale (en-US for example)Tuesday, 6/24/1986, 12:01:02 PM Coordinated Universal Time
primetime().format('WDD, MMMM, DD, HH, mm, ss, TZZ', 'nl-nl')Format the date to the provided date parts and localedinsdag 24-6-1986 12:01:02 Gecoördineerde wereldtijd
primetime().format('{weekday}, {month} {day}, {year} @ {hour}:{minute}:{second} {timezone-short}')Format the date to the provided format and user's locale (en-US for example)Tuesday, June 24, 1986 @ 12:01:02 UTC
primetime().format('{WDD}, {MMMM} {DD}, {YY} @ {HH}:{mm}:{ss} {TZ}', 'nl-nl')Format the date to the provided format and user's localedinsdag, juni 24, 1986 @ 12:01:02 UTC

Comparing dates

If the date()

CodeDescriptionReturns
primetime().difference(primetime().add(1, 'year'), 'days')Compares the difference in days365
primetime().difference(primetime().subtract(1, 'day'), 'hour')Compares the difference in hours-24
primetime().after(primetime().subtract(1, 'month'))Checks if the date is after the given datetrue
primetime().after(primetime().subtract(5, 'days'), 'months')Checks if the date is after the given date in monthsfalse
primetime().after(primetime().subtract(1, 'days'), 'day', true)Checks if the date is equal or after the given date in daystrue
primetime().before(primetime().add(1, 'month'))Checks if the date is before the given datetrue
primetime().before(primetime().add(5, 'days'), 'months')Checks if the date is before the given date in monthsfalse
primetime().before(primetime().add(1, 'days'), 'day', true)Checks if the date is before or equal to the given date in daystrue
primetime().between(primetime().subtract(1, 'day'), primetime().add(1, 'day'))Checks if the date is between the given datestrue
primetime().between(primetime().subtract(1, 'day'), primetime().add(1, 'day'), 'months')Checks if the date is between the given dates in monthsfalse
primetime().between(primetime().subtract(1, 'day'), primetime().add(1, 'day'), 'day', true)Returns if the date is between the given dates in days with equalitytrue
primetime(519998462003).equals(primetime(519998462003)Checks if the date equals the given datetrue
primetime().equals(primetime().add(1, 'month'))Checks if the date equals the given date in monthsfalse

Modifying dates

These methods return the updated PrimeTime object, enabling method chaining.

CodeDescriptionReturns
primetime().add(6, 'days')Adds six days to the current datePrimeTime
primetime().add(5, 'second')Adds five seconds to the current datePrimeTime
primetime().subtract(5, 'minutes')Subtracts 5 minutes from the current datePrimeTime
primetime().subtract(1, 'year')Subtracts one year from the current datePrimeTime
primetime().scale('hours')Removes the minutes, seconds and millisecondsPrimeTime
primetime().scale('month')Removes the days, minutes, seconds and milliseconds from the current datePrimeTime
primetime().clone()Creates a duplicate PrimeTime objectPrimeTime
primetime().clone('day')Creates a duplicate PrimeTime object with the year, month and day setPrimeTime
primetime().update(519998462003)Sets a new timestamp for the objectPrimeTime

Changing time zones

CodeDescriptionReturns
primetime().timezone('Europe/Amsterdam')Sets the object as if it is in that timezonePrimeTime
primetime().timezone('Europe/Amsterdam').format('WDD, MMMM, DD, HH, mm, ss, TZZ')Format the date (in the chosen time zone) to the provided date parts and user's locale (en-US for example)Tuesday, 6/24/1986, 10:01:02 PM Coordinated Universal Time
primetime().timezone('America/New_York').localise('DD, MM, YY, HH, mm, ss', 'nl-nl', 'Europe/Amsterdam')Format the date (in the chosen time zone) to the provided date parts and user's locale (en-US for example) in another time zone24-06-1986 18:01:02
primetime().timezone('Europe/London').after(primetime().timezone('Europe/Amsterdam'))Check if the first date is after the second date, using their respective time zonestrue
primetime().timezone('Europe/London').before(primetime().timezone('Europe/Amsterdam'))Check if the first date is before the second date, using their respective time zonesfalse
primetime().timezone('Europe/London').between(primetime().timezone('Europe/Amsterdam'), primetime().timezone('America/New_York'))Check if the first date is between the other dates, using their respective time zonestrue
primetime().timezone('Europe/London').equal(primetime().timezone('Europe/Amsterdam'))Check if the first date equals the second date, using their respective time zonesfalse

Why Prime Time?

Named after the Alan Parsons Project song Prime Time from the album Ammonia Avenue. This song is one of my personal favorites and its name fits this project quite well.

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.1.2

4 years ago

0.1.0

4 years ago