3.0.8 • Published 4 years ago

protodate v3.0.8

Weekly downloads
1
License
Apache-2.0
Repository
-
Last release
4 years ago

Protodate Logo

npm version build coverage v3.0.8

Better Javascript Dates.

Contents

Intro

ProtoDate is a very fast and lightweight solution for building, parsing, manipulating and formatting Javascript Dates. ProtoDate extends the native JS Date object (without modifying the original prototype) for ease of use and to keep the library light. By leveraging native, low-level code ProtoDate is able to achieve unprecedented speeds.

Compare ProtoDate to Moment.js:

ProtoDateMoment.js
Parses Dates more than 2x fasterSlower
Calculates elapsed time nearly 20x fasterMuch slower
Base lib minified @ 9.6kb (1/5th the size)Minified @ 50.43kb
100% code coverageonly 94.6% code coverage
Timezone support from 1835-2500 (665yr Range)Timezone support from 2012-2022 (10yr Range)

Check out the benchmarks.

Also, if you happened to be a PHP developer, you're in luck because ProtoDate uses the same date formatting as PHP's native date function.

Choose Your File

ProtoDate has 6 different versions so you only have to download what you need.

Here's a helpful flow chart to help you choose which file best fits your needs:

Protodate file chooser flowchart

Usage

Browsers:

Download and include the js file that best fits your needs and include it in your markup.

<script src='protodate.min.js'></script>

Node

Install the library with npm install protodate and require the file that best fits your needs in your script.

const ProtoDate = require('protodate.js');

Formatting Dates

Use the format(formatStr) method to format dates as strings. Use the Formatting Characters Table to build your format string.

Example: new ProtoDate().format("m/d/y g:i a")

Parsing Dates

Use the parse(dateStr[, formatStr]) method to create a ProtoDate object from a string. If you provide a format string to the method, parsing will be much faster, but ProtoDate is smart enough to guess just about any format without it. Use any of the parsable formatting characters to create a format string.

Example: Protodate.parse("January 3rd 2007 @ 4 o'clock")

Manipulating Dates

Use the plus(quantity, period) and minus(quantity, period) methods to add and subtract time from a Date instance.

The period parameter is the unit of time to add or subtract, and the quantity parameter is how many of them to add or subtract. Specify the period parameter with using one of the 6 Date Period Constants, (ie, ProtoDate.MILLISECOND, ProtoDate.SECOND, ProtoDate.MINUTE, ProtoDate.HOUR, ProtoDate.DAY, ProtoDate.YEAR).

Example: new ProtoDate().add(3, ProtoDate.DAY) // 3 days from now

Converting Timezones

This functionality is only available in the `protodate.tz.` files*

You can convert dates to other timezones with the setTimezone(timezone) method.

Example: new ProtoDate().setTimezone('Asia/Hong_Kong').toString() // Current time in Hong Kong

Type Conversion

Generally, you should never need to convert a ProtoDate object to a native Date object since ProtoDate extends Date - it has all the same methods, but these functions are provided as a convenience.

Converting ProtoDate to native Date

Example: const date = protodate.toDate();

Converting Date to ProtoDate

Example: const protodate = ProtoDate.fromDate(date);

Formatting Characters

Each character represents part of a date format string. Characters listed as Parsable are understood by the parse method. All other characters, as well as characters escaped with a \ in the format string will be printed as-is.

format characterDescriptionExample returned valuesParsable
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 week-numbering year. 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
vMilliseconds with leading zeros - 3 Digits.Example: 654
Timezone------
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 50400
Full Date/Time------
cISO 8601 date2004-02-12T15:19:21.990Z
USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT)1533566318
Other------
JCount of days since since noon Universal Time on January 1, 4713 BC on the Julian calendar123412.5
PMoon PhaseWaning Crescent

Timezone Reference

A list of timezones and their corresponding countries:

CountryTimezone
AfghanistanAsia/Kabul
Aland IslandsEurope/Mariehamn
AlbaniaEurope/Tirane
AlgeriaAfrica/Algiers
American SamoaPacific/Pago_Pago
AndorraEurope/Andorra
AngolaAfrica/Luanda
AnguillaAmerica/Anguilla
AntarcticaAntarctica/Casey
AntarcticaAntarctica/Davis
AntarcticaAntarctica/DumontDUrville
AntarcticaAntarctica/Mawson
AntarcticaAntarctica/McMurdo
AntarcticaAntarctica/Palmer
AntarcticaAntarctica/Rothera
AntarcticaAntarctica/Syowa
AntarcticaAntarctica/Troll
AntarcticaAntarctica/Vostok
Antigua and BarbudaAmerica/Antigua
ArgentinaAmerica/Argentina/Buenos_Aires
ArgentinaAmerica/Argentina/Catamarca
ArgentinaAmerica/Argentina/Cordoba
ArgentinaAmerica/Argentina/Jujuy
ArgentinaAmerica/Argentina/La_Rioja
ArgentinaAmerica/Argentina/Mendoza
ArgentinaAmerica/Argentina/Rio_Gallegos
ArgentinaAmerica/Argentina/Salta
ArgentinaAmerica/Argentina/San_Juan
ArgentinaAmerica/Argentina/San_Luis
ArgentinaAmerica/Argentina/Tucuman
ArgentinaAmerica/Argentina/Ushuaia
ArmeniaAsia/Yerevan
ArubaAmerica/Aruba
AustraliaAntarctica/Macquarie
AustraliaAustralia/Adelaide
AustraliaAustralia/Brisbane
AustraliaAustralia/Broken_Hill
AustraliaAustralia/Currie
AustraliaAustralia/Darwin
AustraliaAustralia/Eucla
AustraliaAustralia/Hobart
AustraliaAustralia/Lindeman
AustraliaAustralia/Lord_Howe
AustraliaAustralia/Melbourne
AustraliaAustralia/Perth
AustraliaAustralia/Sydney
AustriaEurope/Vienna
AzerbaijanAsia/Baku
BahamasAmerica/Nassau
BahrainAsia/Bahrain
BangladeshAsia/Dhaka
BarbadosAmerica/Barbados
BelarusEurope/Minsk
BelgiumEurope/Brussels
BelizeAmerica/Belize
BeninAfrica/Porto-Novo
BermudaAtlantic/Bermuda
BhutanAsia/Thimphu
BoliviaAmerica/La_Paz
Bonaire, Saint Eustatius and SabaAmerica/Kralendijk
Bosnia and HerzegovinaEurope/Sarajevo
BotswanaAfrica/Gaborone
BrazilAmerica/Araguaina
BrazilAmerica/Bahia
BrazilAmerica/Belem
BrazilAmerica/Boa_Vista
BrazilAmerica/Campo_Grande
BrazilAmerica/Cuiaba
BrazilAmerica/Eirunepe
BrazilAmerica/Fortaleza
BrazilAmerica/Maceio
BrazilAmerica/Manaus
BrazilAmerica/Noronha
BrazilAmerica/Porto_Velho
BrazilAmerica/Recife
BrazilAmerica/Rio_Branco
BrazilAmerica/Santarem
BrazilAmerica/Sao_Paulo
British Indian Ocean TerritoryIndian/Chagos
British Virgin IslandsAmerica/Tortola
BruneiAsia/Brunei
BulgariaEurope/Sofia
Burkina FasoAfrica/Ouagadougou
BurundiAfrica/Bujumbura
CambodiaAsia/Phnom_Penh
CameroonAfrica/Douala
CanadaAmerica/Atikokan
CanadaAmerica/Blanc-Sablon
CanadaAmerica/Cambridge_Bay
CanadaAmerica/Creston
CanadaAmerica/Dawson
CanadaAmerica/Dawson_Creek
CanadaAmerica/Edmonton
CanadaAmerica/Fort_Nelson
CanadaAmerica/Glace_Bay
CanadaAmerica/Goose_Bay
CanadaAmerica/Halifax
CanadaAmerica/Inuvik
CanadaAmerica/Iqaluit
CanadaAmerica/Moncton
CanadaAmerica/Nipigon
CanadaAmerica/Pangnirtung
CanadaAmerica/Rainy_River
CanadaAmerica/Rankin_Inlet
CanadaAmerica/Regina
CanadaAmerica/Resolute
CanadaAmerica/St_Johns
CanadaAmerica/Swift_Current
CanadaAmerica/Thunder_Bay
CanadaAmerica/Toronto
CanadaAmerica/Vancouver
CanadaAmerica/Whitehorse
CanadaAmerica/Winnipeg
CanadaAmerica/Yellowknife
Cape VerdeAtlantic/Cape_Verde
Cayman IslandsAmerica/Cayman
Central African RepublicAfrica/Bangui
ChadAfrica/Ndjamena
ChileAmerica/Punta_Arenas
ChileAmerica/Santiago
ChilePacific/Easter
ChinaAsia/Shanghai
ChinaAsia/Urumqi
Christmas IslandIndian/Christmas
Cocos IslandsIndian/Cocos
ColombiaAmerica/Bogota
ComorosIndian/Comoro
Cook IslandsPacific/Rarotonga
Costa RicaAmerica/Costa_Rica
CroatiaEurope/Zagreb
CubaAmerica/Havana
CuraçaoAmerica/Curacao
CyprusAsia/Famagusta
CyprusAsia/Nicosia
Czech RepublicEurope/Prague
Democratic Republic of the CongoAfrica/Kinshasa
Democratic Republic of the CongoAfrica/Lubumbashi
DenmarkEurope/Copenhagen
DjiboutiAfrica/Djibouti
DominicaAmerica/Dominica
Dominican RepublicAmerica/Santo_Domingo
East TimorAsia/Dili
EcuadorAmerica/Guayaquil
EcuadorPacific/Galapagos
EgyptAfrica/Cairo
El SalvadorAmerica/El_Salvador
Equatorial GuineaAfrica/Malabo
EritreaAfrica/Asmara
EstoniaEurope/Tallinn
EthiopiaAfrica/Addis_Ababa
Falkland IslandsAtlantic/Stanley
Faroe IslandsAtlantic/Faroe
FijiPacific/Fiji
FinlandEurope/Helsinki
FranceEurope/Paris
French GuianaAmerica/Cayenne
French PolynesiaPacific/Gambier
French PolynesiaPacific/Marquesas
French PolynesiaPacific/Tahiti
French Southern TerritoriesIndian/Kerguelen
GabonAfrica/Libreville
GambiaAfrica/Banjul
GeorgiaAsia/Tbilisi
GermanyEurope/Berlin
GermanyEurope/Busingen
GhanaAfrica/Accra
GibraltarEurope/Gibraltar
GreeceEurope/Athens
GreenlandAmerica/Danmarkshavn
GreenlandAmerica/Godthab
GreenlandAmerica/Scoresbysund
GreenlandAmerica/Thule
GrenadaAmerica/Grenada
GuadeloupeAmerica/Guadeloupe
GuamPacific/Guam
GuatemalaAmerica/Guatemala
GuernseyEurope/Guernsey
GuineaAfrica/Conakry
Guinea-BissauAfrica/Bissau
GuyanaAmerica/Guyana
HaitiAmerica/Port-au-Prince
HondurasAmerica/Tegucigalpa
Hong KongAsia/Hong_Kong
HungaryEurope/Budapest
IcelandAtlantic/Reykjavik
IndiaAsia/Kolkata
IndonesiaAsia/Jakarta
IndonesiaAsia/Jayapura
IndonesiaAsia/Makassar
IndonesiaAsia/Pontianak
IranAsia/Tehran
IraqAsia/Baghdad
IrelandEurope/Dublin
Isle of ManEurope/Isle_of_Man
IsraelAsia/Jerusalem
ItalyEurope/Rome
Ivory CoastAfrica/Abidjan
JamaicaAmerica/Jamaica
JapanAsia/Tokyo
JerseyEurope/Jersey
JordanAsia/Amman
KazakhstanAsia/Almaty
KazakhstanAsia/Aqtau
KazakhstanAsia/Aqtobe
KazakhstanAsia/Atyrau
KazakhstanAsia/Oral
KazakhstanAsia/Qyzylorda
KenyaAfrica/Nairobi
KiribatiPacific/Enderbury
KiribatiPacific/Kiritimati
KiribatiPacific/Tarawa
KuwaitAsia/Kuwait
KyrgyzstanAsia/Bishkek
LaosAsia/Vientiane
LatviaEurope/Riga
LebanonAsia/Beirut
LesothoAfrica/Maseru
LiberiaAfrica/Monrovia
LibyaAfrica/Tripoli
LiechtensteinEurope/Vaduz
LithuaniaEurope/Vilnius
LuxembourgEurope/Luxembourg
MacaoAsia/Macau
MacedoniaEurope/Skopje
MadagascarIndian/Antananarivo
MalawiAfrica/Blantyre
MalaysiaAsia/Kuala_Lumpur
MalaysiaAsia/Kuching
MaldivesIndian/Maldives
MaliAfrica/Bamako
MaltaEurope/Malta
Marshall IslandsPacific/Kwajalein
Marshall IslandsPacific/Majuro
MartiniqueAmerica/Martinique
MauritaniaAfrica/Nouakchott
MauritiusIndian/Mauritius
MayotteIndian/Mayotte
MexicoAmerica/Bahia_Banderas
MexicoAmerica/Cancun
MexicoAmerica/Chihuahua
MexicoAmerica/Hermosillo
MexicoAmerica/Matamoros
MexicoAmerica/Mazatlan
MexicoAmerica/Merida
MexicoAmerica/Mexico_City
MexicoAmerica/Monterrey
MexicoAmerica/Ojinaga
MexicoAmerica/Tijuana
MicronesiaPacific/Chuuk
MicronesiaPacific/Kosrae
MicronesiaPacific/Pohnpei
MoldovaEurope/Chisinau
MonacoEurope/Monaco
MongoliaAsia/Choibalsan
MongoliaAsia/Hovd
MongoliaAsia/Ulaanbaatar
MontenegroEurope/Podgorica
MontserratAmerica/Montserrat
MoroccoAfrica/Casablanca
MozambiqueAfrica/Maputo
MyanmarAsia/Yangon
NamibiaAfrica/Windhoek
NauruPacific/Nauru
NepalAsia/Kathmandu
NetherlandsEurope/Amsterdam
New CaledoniaPacific/Noumea
New ZealandPacific/Auckland
New ZealandPacific/Chatham
NicaraguaAmerica/Managua
NigerAfrica/Niamey
NigeriaAfrica/Lagos
NiuePacific/Niue
Norfolk IslandPacific/Norfolk
North KoreaAsia/Pyongyang
Northern Mariana IslandsPacific/Saipan
NorwayEurope/Oslo
OmanAsia/Muscat
PakistanAsia/Karachi
PalauPacific/Palau
Palestinian TerritoryAsia/Gaza
Palestinian TerritoryAsia/Hebron
PanamaAmerica/Panama
Papua New GuineaPacific/Bougainville
Papua New GuineaPacific/Port_Moresby
ParaguayAmerica/Asuncion
PeruAmerica/Lima
PhilippinesAsia/Manila
PitcairnPacific/Pitcairn
PolandEurope/Warsaw
PortugalAtlantic/Azores
PortugalAtlantic/Madeira
PortugalEurope/Lisbon
Puerto RicoAmerica/Puerto_Rico
QatarAsia/Qatar
Republic of the CongoAfrica/Brazzaville
ReunionIndian/Reunion
RomaniaEurope/Bucharest
RussiaAsia/Anadyr
RussiaAsia/Barnaul
RussiaAsia/Chita
RussiaAsia/Irkutsk
RussiaAsia/Kamchatka
RussiaAsia/Khandyga
RussiaAsia/Krasnoyarsk
RussiaAsia/Magadan
RussiaAsia/Novokuznetsk
RussiaAsia/Novosibirsk
RussiaAsia/Omsk
RussiaAsia/Sakhalin
RussiaAsia/Srednekolymsk
RussiaAsia/Tomsk
RussiaAsia/Ust-Nera
RussiaAsia/Vladivostok
RussiaAsia/Yakutsk
RussiaAsia/Yekaterinburg
RussiaEurope/Astrakhan
RussiaEurope/Kaliningrad
RussiaEurope/Kirov
RussiaEurope/Moscow
RussiaEurope/Samara
RussiaEurope/Saratov
RussiaEurope/Simferopol
RussiaEurope/Ulyanovsk
RussiaEurope/Volgograd
RwandaAfrica/Kigali
Saint BarthélemyAmerica/St_Barthelemy
Saint HelenaAtlantic/St_Helena
Saint Kitts and NevisAmerica/St_Kitts
Saint LuciaAmerica/St_Lucia
Saint MartinAmerica/Marigot
Saint Pierre and MiquelonAmerica/Miquelon
Saint Vincent and the GrenadinesAmerica/St_Vincent
SamoaPacific/Apia
San MarinoEurope/San_Marino
Sao Tome and PrincipeAfrica/Sao_Tome
Saudi ArabiaAsia/Riyadh
SenegalAfrica/Dakar
SerbiaEurope/Belgrade
SeychellesIndian/Mahe
Sierra LeoneAfrica/Freetown
SingaporeAsia/Singapore
Sint MaartenAmerica/Lower_Princes
SlovakiaEurope/Bratislava
SloveniaEurope/Ljubljana
Solomon IslandsPacific/Guadalcanal
SomaliaAfrica/Mogadishu
South AfricaAfrica/Johannesburg
South Georgia and the South Sandwich IslandsAtlantic/South_Georgia
South KoreaAsia/Seoul
South SudanAfrica/Juba
SpainAfrica/Ceuta
SpainAtlantic/Canary
SpainEurope/Madrid
Sri LankaAsia/Colombo
SudanAfrica/Khartoum
SurinameAmerica/Paramaribo
Svalbard and Jan MayenArctic/Longyearbyen
SwazilandAfrica/Mbabane
SwedenEurope/Stockholm
SwitzerlandEurope/Zurich
SyriaAsia/Damascus
TaiwanAsia/Taipei
TajikistanAsia/Dushanbe
TanzaniaAfrica/Dar_es_Salaam
ThailandAsia/Bangkok
TogoAfrica/Lome
TokelauPacific/Fakaofo
TongaPacific/Tongatapu
Trinidad and TobagoAmerica/Port_of_Spain
TunisiaAfrica/Tunis
TurkeyEurope/Istanbul
TurkmenistanAsia/Ashgabat
Turks and Caicos IslandsAmerica/Grand_Turk
TuvaluPacific/Funafuti
U.S. Virgin IslandsAmerica/St_Thomas
UgandaAfrica/Kampala
UkraineEurope/Kiev
UkraineEurope/Uzhgorod
UkraineEurope/Zaporozhye
United Arab EmiratesAsia/Dubai
United KingdomEurope/London
United StatesAmerica/Adak
United StatesAmerica/Anchorage
United StatesAmerica/Boise
United StatesAmerica/Chicago
United StatesAmerica/Denver
United StatesAmerica/Detroit
United StatesAmerica/Indiana/Indianapolis
United StatesAmerica/Indiana/Knox
United StatesAmerica/Indiana/Marengo
United StatesAmerica/Indiana/Petersburg
United StatesAmerica/Indiana/Tell_City
United StatesAmerica/Indiana/Vevay
United StatesAmerica/Indiana/Vincennes
United StatesAmerica/Indiana/Winamac
United StatesAmerica/Juneau
United StatesAmerica/Kentucky/Louisville
United StatesAmerica/Kentucky/Monticello
United StatesAmerica/Los_Angeles
United StatesAmerica/Menominee
United StatesAmerica/Metlakatla
United StatesAmerica/New_York
United StatesAmerica/Nome
United StatesAmerica/North_Dakota/Beulah
United StatesAmerica/North_Dakota/Center
United StatesAmerica/North_Dakota/New_Salem
United StatesAmerica/Phoenix
United StatesAmerica/Sitka
United StatesAmerica/Yakutat
United StatesPacific/Honolulu
United States Minor Outlying IslandsPacific/Midway
United States Minor Outlying IslandsPacific/Wake
UruguayAmerica/Montevideo
UzbekistanAsia/Samarkand
UzbekistanAsia/Tashkent
VanuatuPacific/Efate
VaticanEurope/Vatican
VenezuelaAmerica/Caracas
VietnamAsia/Ho_Chi_Minh
Wallis and FutunaPacific/Wallis
Western SaharaAfrica/El_Aaiun
YemenAsia/Aden
ZambiaAfrica/Lusaka
ZimbabweAfrica/Harare

License

ProtoDate comes with an Apache 2.0 license. Read the license here.

3.0.8

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

2.0.15

6 years ago

2.0.12

6 years ago

2.0.11

6 years ago

1.1.17

6 years ago

1.1.1

6 years ago

1.0.0

6 years ago