0.0.3 • Published 1 year ago

john-titor v0.0.3

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

John Titor

John Titor is a zero-dependency JavaScript library for handling date-time string formatting the PHP way. Named after time traveler TimeTravel_0, it has been designed to both make date string formatting easier in-general as well as make PHP developers feel more at-home in the JavaScript ecosystem.

Installation

This library can be installed via npm:

npm install john-titor

Once installed it can be used in your project via either ESM or CJS as-needed:

import titor from 'john-titor';

// OR

const titor = require('john-titor');

If you'd like to include this in a web page, you can download the source code here. Heck, in-a-pinch, you would be able to load it into your page with the following to start experimenting with it:

<!-- John Titor date formatter: -->
<script src="https://raw.githubusercontent.com/WarrenUhrich/john-titor/main/lib/titor.js"></script>

How to use John Titor

This library comes with a function called titor.

Parameters:

  • format: a string containing special characters from the legend found below
  • (optional) date: a JavaScript Date object that you'd like to format as a string—if no date is provided, it will use the present date and time instead

Return:

  • string: the provided date as a formatted string

Example Usage

// By default it will format the current time:

const currentYear  = titor('Y');       // '2000'
const shortYear    = titor('y');       // '00'

const americanDate = titor('m/d/Y');   // '11/02/2000'
const betterDate   = titor('Y-m-d');   // '2000-11-02'

const wordDate     = titor('F jS, Y'); // 'November 2nd, 2000'

const dayAndTime   = titor('l g:i a'); // 'Thursday 3:04 am'

// If you'd like, you can pass in a specific time to format:

const date = new Date('2036-03-14 16:05'); // Date to try out!

const americanizeDate = titor('m/d/Y',             date); // '03/14/2036'
const unixTimestamp   = titor('U',                 date); // '2089145100'
const bigDateTime     = titor('l F jS, Y @ g:i A', date); // 'Friday March 14th, 2036 @ 4:05 PM'

// You can include words, phrases, and more in the output;
// be mindful that you escape any letters that overlap
// this library's legend characters! Use a backslash: \

const formattedOutput = titor(String.raw`

\H\e\l\l\o, \W\o\r\l\d!
=============

\D\a\t\e \I\n\f\o:
----------

\D\a\y:   l
\M\o\n\t\h: F
\Y\e\a\r:  Y
\U\n\ix:  U

`, date);

// formattedOutput would contain the following (sans comments):
/*


Hello, World!
=============

Date Info:
----------

Day:   Friday
Month: March
Year:  2036
Unix:  2089145100


*/

Quick Legend

Year

  • Y: four-digit year number
  • y: two-digit year number
  • L: 1 if a leap year, 0 if not

Month

  • m: month number (with leading zero)
  • n: month number (without leading zero)
  • F: full textual name of the month
  • M: three-letter textual name of month
  • t: number of days in the month

Day

  • d: day of month (with leading zero)
  • j: day of month (without leading zero)
  • l: full textual day of the week
  • D: three-letter textual day of the week

Hour

  • h: 12-hour clock hour (with leading zero)
  • g: 12-hour clock hour (without leading zero)
  • a: lower-case am or pm
  • A: upper-case AM or PM
  • H: 24-hour clock hour (with leading zero)
  • G: 24-hour clock hour (without leading zero)
  • I: 1 if in daylight savings time, 0 if not

Minute

  • i: minutes (with leading zero)

Second

  • s: seconds (with leading zero)
  • u: microseconds
  • U: number of seconds since the Unix Epoch

Complete Legend

FormatDescription
dThe day of the month (from 01 to 31)
DA textual representation of a day (three letters)
jThe day of the month without leading zeros (1 to 31)
lA full textual representation of a day
NThe ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
SThe English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
wA numeric representation of the day (0 for Sunday, 6 for Saturday)
zThe day of the year (from 0 through 365)
WThe ISO-8601 week number of year (weeks starting on Monday)
FA full textual representation of a month (January through December)
mA numeric representation of a month (from 01 to 12)
MA short textual representation of a month (three letters)
nA numeric representation of a month, without leading zeros (1 to 12)
tThe number of days in the given month
LWhether it's a leap year (1 if it is a leap year, 0 otherwise)
oThe ISO-8601 year number
YA four digit representation of a year
yA two digit representation of a year
aLowercase am or pm
AUppercase AM or PM
BSwatch Internet time (000 to 999)
g12-hour format of an hour (1 to 12)
G24-hour format of an hour (0 to 23)
h12-hour format of an hour (01 to 12)
H24-hour format of an hour (00 to 23)
iMinutes with leading zeros (00 to 59)
sSeconds, with leading zeros (00 to 59)
uMicroseconds (added in PHP 5.2.2)
eThe timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
IWhether the date is in daylight savings time (1 if Daylight Savings Time, 0 otherwise)
ODifference to Greenwich time (GMT) in hours (Example: +0100)
PDifference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
TTimezone abbreviations (Examples: EST, MDT)
ZTimezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
cThe ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
rThe RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
UThe seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

Incomplete Features

There are thirty-seven (37) internal methods required to match the formatting features supplied by PHP. Some features have not yet reached parity with its PHP counterpart:

  • e
  • O
  • P
  • T
  • Z
  • c
  • r

Most of the above provide some reasonable output in the mean-time, but these will require more work to better handle timezone cases as well as behave more 1:1 with PHP date formatting.

Author

My name is Warren Uhrich! I'm a human person; instructor; and world wide web developer from Canada. Despite its potentially confusing syntax, I've always enjoyed the flexibility of PHP's date formatting. The ability to prepare a simple template out of a set of characters is powerful!

My goal when building john-titor was to develop a zero-dependancy library that is easy to use and brings the aforementioned power to JavaScript. Wield it wisely!

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago