1.3.4 • Published 2 years ago

date-mirror v1.3.4

Weekly downloads
1
License
ISC
Repository
-
Last release
2 years ago

date-mirror

Like the native date function in PHP but modified to include date ranges and plain text.

const date = require('date-mirror')
date('F jS, Y')

Installation

This is a simple JavaScript module available through the npm registry.

Installation is done using the npm install command:

npm install date-mirror

Or in the browser by loading the library from unpkg and using the date() variable:

<script src='https://unpkg.com/date-mirror'></script>

Usage

This is the function's signature:

date (template: string, start: string = null, end: string = null): string

It can be used to retrieve and format today's date:

date('Y') // 2020
date('n/j/y') // 1/1/20
date('m-d-Y') // 01-01-2020
date('F jS, Y') // January 1st, 2020

Format a specific date:

date('F jS, Y', '07/04/1776') // July 4th, 1776
date('n/j/y', '25 March 1807') // 3/25/07
date('D, F jS', '8-18-1920') // Wed, August 18th

Or format a range of dates:

date('F jS - jS, Y', '7/16/69', '7/24/69') // July 16th - 24th, 1969
date('m/d/Y [until] m/d/Y', 'Sep 1 1939', 'Sep 2 1945') // 09/01/1939 until 09/02/1945

A complete list of accepted parameters is below.

Parameters

Below is a complete list of the parameters that can be used inside of the function.

CharacterDescriptionReturns
dDay of the month with leading zeros01 to 31
DTextual representation of the week daySun through Sat
jDay of the month - no leading zeroes1 to 31
lTextual representation of the day of the weekSunday through Saturday
NISO-8601 numeric representation of the day of the week1 (for Monday) through 7 (for Sunday)
SEnglish ordinal suffix for the day of the monthst, nd, rd or th
wNumeric representation of the day of the week0 (for Sunday) through 6 (for Saturday)
zThe day of the year starting from 00 through 365
WISO-8601 week number of yeare.g. 12, 40
FTextual representation of a monthJanuary through December
mNumeric representation of a month with leading zeros01 through 12
MShort textual representation of a monthJan through Dec
nNumeric representation of a month without leading zeros1 through 12
tNumber of days in the given month28 through 31
LWhether it's a leap yeartrue or false
oISO-8601 week-numbering yeare.g. 2008, 2020
YFull numeric representation of a yeare.g. 2008, 2020
y2-digit representation of a yeare.g. 08, 20
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM
g12-hour format without leading zeros1 through 12
G24-hour format without leading zeros0 through 23
h12-hour format with leading zeros01 through 12
H24-hour format with leading zeros00 through 23
iMinutes with leading zeros00 through 59
KMinutes without leading zeros0 through 59
sSeconds with leading zeros00 through 59
PSeconds without leading zeros0 through 59
vMilliseconds, 3-digit789
eTimezone nameEastern Standard Time
TTimezone abbreviationEST
IWhether or not date is in daylight savings timetrue or false
ZTimezone offset in minutes-720 through 840
cISO 8601 date2020-01-01T12:30:30.000+05:00
rRFC 2822 compliant dateSun Nov 01 2020 02:00:00 GMT-0500 (Eastern Standard Time)
USeconds since the Unix Epoch1970 was a long time ago, so ...

Plain text can also be added and needs to be between open- and closing-brackets: []. Anything in between the brackets will be treated as plain text and returned along with the dates.

How It Works

Returned strings are meant to perfectly mirror the inputted format (including special characters and spaces).

date('&^%$') // &^%$
date('pie') // pie

The reason why the above parameters returned an exact match of what was inputted is because those characters aren't valid date parameters (see table above). If the function detects a valid date parameter, it will substitute the character for the relevant date value. If not, it will just return the character.

Date Ranges

You can return date ranges by inputting 2 additional parameters:

date('F jS - jS, Y', '7/16/69', '7/24/69') // July 16th - 24th, 1969
date('m/d/Y [until] m/d/Y', 'Sep 1 1939', 'Sep 2 1945') // 09/01/1939 until 09/02/1945

When building the formatting string, know that the first occurrence of a valid date character will call the 1st date and a repeat of the same character will then call the 2nd date:

date('m - m', '1-5-20', '3-8-20') // 01 - 03
date('m/d - m/d', '1-5-20', '3-8-20') // 01/05 - 03/08

But keep in mind that the returned string will mirror the format. That could create a problem like this:

date('F jS - jS', '1-5-20', '3-8-20') // January 5th - 8th

Where the returned string suggests a date range of January 5th - 8th even though the literal date range is January 5th through March 8th.

We may decide to build a function to fix this in the future but, for now, we recommend doing something like this to review the date range first and then building a format based on the results:

const date1 = '1-5-20'
const date2 = '3-8-20'
if (date('F', date1) == date('F', date2)) {
  console.log(date('F jS - jS', date1, date2))
} else {
  console.log(date('F jS - F jS', date1, date2)) // January 5th - March 8th
}

Plain Text

Sometimes you want to include some text between dates in a range:

date('M jS - jS', '1/5/20', '1/8/20') // Jan 5th - 8th
date('M jS [through] jS', '1/5/20', '1/8/20') // Jan 5th through 8th
date('M jS [until the] jS', '1/5/20', '1/8/20') // Jan 5th until the 8th

This can be done one of 2 ways - either by including a character not included in the list of parameters above (see table) like the hyphen used in the first example, or by putting any desired plain text inside of opening- and closing-brackets '[]'.

Gotchas

A few things to watch out for when inputting strings ...

  1. If you don't include a year with your input, then it'll assume the current year.
  2. If returning a range, check for matching months or years first before building your formatting string.
  3. We would recommend getting in the habit of putting plain text between brackets '[]'. You don't want to make the mistake of including a valid date parameter in text that you want to be printed as plain and getting a result like this:
date('M jS to jS', '1/5/20', '1/8/20') // Jan 5th 312020 8th

New

Date-Mirror v1.3 and up now supports date string inputs in YYYY-MM-DD format as opposed to the native Date function in JavaScript.

date('M', '2020-01-01') // Jan
date('M', '2020/01/01') // Jan

Previously, and in native JavaScript, this would produce inconsistent results.

People

The author and maintainer of date-mirror is Tyler Willis. Support me on Patreon!

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.2.9

4 years ago

1.2.10

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.1.11

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.10

4 years ago

1.1.3

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago