2.0.0 • Published 4 years ago

timeUtils v2.0.0

Weekly downloads
1,469
License
BSD-2-Clause
Repository
github
Last release
4 years ago

Build Status Conventional Commits npm version

timeUtils.js

Simple time/date formatter for JavaScript which uses PHP-like syntax to easily format JavaScript date objects into legible/formatted strings. It works in the browser and Node.js and is quite small ~1.3KB (minified & gzipped).

Installation & Including the Script:

First, install the module via NPM:

npm i timeUtils1

Then include the module...

As an import:

import { formatDate } from 'timeUtils'

As a Common JS library:

const formatDate = require('timeUtils').formatDate; 

As a script tag:

<script type="text/javascript" src="node_modules/timeUtils/dist/timeUtils.umd.js" />

Usage:

timeUtils's formatDate function expects a JavaScript Date object as its first argument and a string-based template as its second argument.

The templates should take the following form: "Lorem Ipsum '#{m}/#{d}/#{Y}'"

Where strings wrapped in #{} represent date/time variables which will be formatted for you. The table below shows all the variables currently available along with a brief description. In the above example template, we would get date in the "mm/dd/yyyy" format - for example.

Date Variables

Symbol/TokenTypeDescriptionExample(s)
Dday of weekTextual representation of day of week, 3 lettersSun, Mon, ... Sat
lday of weekFull textual representation of day of weekSunday, Monday, ... Saturday
FmonthFull textual representation of the monthJanuary, ... December
MmonthAbbreviated textual representation of month, 3 lettersJan, Feb, ... Dec
nmonthNumeric representation of month without leading 0's1, 2, ... 12
mmonthNumeric representation of the month with leading 0's01, 02, ... 12
jday of monthDay of month, without leading 0's1, 2, ... 31
dday of monthDay of the month, 2 digits, with leading zeros01, 02, ... 31
yyearShort numeric year, 2 digits00, 01, ... 15
YyearFull numeric year, 4 digits2000, 2001, ... 2015

Time Variables

Symbol/TokenTypeDescriptionExample(s)
ghour12-hour format of the hour, without leading 0's1, 2, ... 12
hhour12-hour format of the hour, with leading 0's01, 02, ... 12
Ghour24-hour format of the hour, without leading 0's0, 1, ... 23
Hhour24-hour format of the hour, with leading 0's00, 01, ... 23
iminuteMinutes with leading 0's00, 01, ... 59
ssecondsSeconds with leading 0's00, 01, ... 59
aam/pmLowercase ante meridiem and post meridiem'am' or 'pm'
Aam/pmUppercase ante meridiem and post meridiem'AM' or 'PM'

Internationalization:

timeUtils provides an internationalize method which can be used to support multiple languages:

import { internationalize } from 'timeUtils';
const daysOfWeek = [
  [ 'Domingo', 'Dom' ],
  [ 'Lunes', 'Lun' ],
  [ 'Martes', 'Mar' ],
  [ 'Miércoles', 'Mié' ],
  [ 'Jueves', 'Jue' ],
  [ 'Viernes', 'Vie' ],
  [ 'Sábado', 'Sáb' ],
];
const monthsOfYear = [
  [ 'Enero', 'Ene' ],
  [ 'Febrero', 'Feb' ],
  [ 'Marzo', 'Mar' ],
  [ 'Abril', 'Abr' ],
  [ 'Mayo', 'May' ],
  [ 'Junio', 'Jun' ],
  [ 'Julio', 'Jul' ],
  [ 'Agosto', 'Ago' ],
  [ 'Septiembre', 'Sep' ],
  [ 'Octubre', 'Oct' ],
  [ 'Noviembre', 'Nov' ],
  [ 'Diciembre', 'Dic' ],
];
internationalize({ daysOfWeek, monthsOfYear });

Examples

var date = new Date()

Mon Apr 06 2015 21:51:08 GMT-0600 (Mountain Daylight Time)

timeUtils.formatDate(date,'Your appointment is on #{l}, #{F} #{j}, #{Y} at #{g}:#{i} #{A}.')

"Your appointment is on Monday, April 6, 2015 at 9:48 PM."

1 add --save if you are using npm < 5.0.0