🚀 Launch Week Day 2:Introducing Custom Tabs for Org Alerts.Learn More →
Socket
Book a DemoInstallSign in
Socket

date.format.js

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

date.format.js

A JavaScript date format library that uses the same method as PHP's date() function.

Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
6
200%
Maintainers
1
Weekly downloads
 
Created
Source

date.format

This was developed to allow for the formatting of dates in JavaScript and ActionScript like PHP can do. I actually just took the documentation from the PHP date function and went down the list implementing every option that I could do easily. I know more support could be added, but I didn’t need it (and still don’t). If anyone embellishes on this let me know and we can post better versions!

Using it is simple, but you may need to refer to the available format string options often to remember how to use it. I always need to refer to PHP’s date documentation every time I use it. You may also escape letters to make them literal, but be sure to escape your escape so Javascript can handle the string correctly.

Installation

  • Download the latest release (zipped).
  • Clone the repo: git clone https://github.com/jacwright/date.format
  • Install with npm: npm install date.format.js
  • Install with yarn: yarn add date.format.js
  • Install with Composer: composer require kevit-development/date.format.js
  • Install with Bower: bower install date.format.js

Usage

var myDate = new Date();
alert(myDate.format('d-m-Y')); // Outputs "03-08-2017"
alert(myDate.format('M jS, Y')); // Outputs "Dec 16th, 2013"
alert(myDate.format('M jS, Y \\i\\s \\h\\e\\r\\e!')); // Outputs "Dec 16th, 2013 is here!"

Localization

Include your preferred locale.js from the locales folder and require it before you require date.format.

<script src="locale.js"></script>
<script src="date.format"></script>

Localization is supported for the following identifiers:

  • D
  • l (lowercase L)
  • F
  • M

Supported identifiers

Taken from http://php.net/manual/en/function.date.php and modified.

Format characterDescriptionExample
Day
dDay of the month, 2 digits with leading zeros01 to 31
DA textual representation of a day, three lettersMon through Sun
jDay of the month without leading zeros1 to 31
l (lowercase L)A full textual 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 month, 2 charactersst, nd, rd or th. Works well with j
wNumeric representation of the day of the week0 (for Sunday) through 6 (for Saturday)
zThe day of the year (starting from 0)0 through 365
Week
WISO-8601 week number of year, weeks starting on MondayExample: 42 (the 42nd week in the year)
Month
FA full textual representation of a month, such as January or MarchJanuary through December
mNumeric representation of a month, with leading zeros01 through 12
MA short textual representation of a month, three lettersJan through Dec
nNumeric representation of a month, without leading zeros1 through 12
tNumber of days in the given month28 through 31
Year
LWhether it’s a leap year1 if it is a leap year, 0 otherwise.
oISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.Examples: 1999 or 2003
YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
yA two digit representation of a yearExamples: 99 or 03
Time
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM
BSwatch Internet time000 through 999
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
sSeconds, with leading zeros00 through 59
vMillisecondsExample: 654
Timezone
eTimezone identifierAtlantic/Azorest, Europe/London
I (capital i)Whether or not the date is in daylights savings time1 if Daylight Savings Time, 0 otherwise.
ODifference to Greenwich time (GMT) in hoursExample: +0200
PDifference to Greenwich time (GMT) with colon between hours and minutesExample: +02:00
TTimezone setting of this machineExamples: EST, MDT
ZTimezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.-43200 through 43200
Full Date/Time
cISO 8601 formatted date2004-02-12T15:19:21+00:00
rRFC 2822 formatted dateExample: Thu, 21 Dec 2000 16:01:07 +0200
USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT)Example: 1501804996

Contributing

Date.format is an open-source library and we would love for you to contribute! Head over to CONTRIBUTING.md.

FAQs

Package last updated on 04 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts