0.3.2 • Published 2 years ago

jdx v0.3.2

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

jDx

Extension for JavaScript date (format and parse)

Npm version   Npm download   Build Status   Coverage Status   Dependencies

Installation

jDx library have been tested on NodeJS and on most browsers.

On node:

npm install jdx

On browser:

<script src="/dist/js/jdx.min.js" integrity="sha384-tj1IdTxBhXxBdA0/P1vSAM86XtU0Rtl2mDdEkYk9UTDLIDq+DiodnY/IdhP2Pt/n"></script>

Usage

The library provide one item with one function to format dates and another to parse dates.

Format dates:

  • jDx.formatDate(mask);
  • jDx.formatDate(date[, mask [, utc]]);

  • date: Provide a javascript Date object to format. Default to new Date().

  • mask: The format requested can be either a name or an expression. See Patterns and Language to know more. Default is 'default'.
  • utc: Boolean value, if true will display UTC time, else local time. Default is false.
var jDx = require('jdx');

jDx.formatDate(new Date()); // return 'Sun Sep 25 2016 13:06:24'
jDx.formatDate(new Date(), 'mmmm yyyy'); // return 'September 2016'
jDx.formatDate('isoUtcDateTime'); // return '2016-09-25T11:06:24Z'

Parse dates:

  • jDx.parseDate(date[, mask [, utc]]);

  • date: A string representing a date.

  • mask: The format used by the string representation. Can be either a name or an expression. If none is provided the function will try with known pattern including previously used. See Patterns and Language to know more.
  • utc: Boolean value, if true will consider date as in UTC time, else in local time. Default is false.
var jDx = require('jdx');

jDx.formatDate('Sun Sep 25 2016 13:06:24'); // equals to `Sun Sep 25 2016 13:06:24 GMT+0200`
jDx.formatDate('September 2016', 'mmmm yyyy'); // equals to `Sun Sep 01 2016 00:00:00 GMT+0200`
jDx.formatDate('2016-09-25T11:06:24Z'); // equals to `Sun Sep 25 2016 13:06:24 GMT+0200`

Patterns

The expression provide to those previous functions will recognize those patterns

  • d: The day of the month.
  • dd: The day of the month on two digits.
  • ddd: A short representation of the day of the week (3 characters).
  • dddd: The name of the day of the week.
  • m: The number of the month.
  • mm: The number of the month on two digits.
  • mmm: A short representation of the month.
  • mmmm: The name of the month.
  • yy: The last two digits of the current year.
  • yyyy: The year on four digits.
  • h: The hours on a 12 hours clock.
  • hh: The hours on a 12 hours clock on two digits.
  • H: The hours on a 24 hours clock.
  • HH: The hours on a 24 hours clock on two digits.
  • M: The minutes.
  • MM: The minutes on two digits.
  • s: The seconds.
  • ss: The seconds on two digits.
  • l: The milliseconds on three digits.
  • L: The hundreads of seconds on two digits.
  • t: The meridiem (a or p)
  • tt: The meridiem (am or pm)
  • T: The meridiem (A or P)
  • TT: The meridiem (AM or PM)
  • Z: The local time offset (UTC or GMT+0000)
  • o: The local time offset in minutes
  • O: The local time offset in the form '+HHMM'
  • S: Day of the month suffix (th, st, nd or rd).

To include extra characters, put them into quote ''.

The library alreay know some default pattern. See Languages.

Languages

The library support english as default language. However it's possible to configure the library differently.

  • jDx.setLang(lang): Change the days and month names used by the library. Some pattern might change too.

Note that pattern are more localization (l10n) than internationalization (i18n) however the library is not mature enough to handle the difference perfectly but still provide way to change the formats availables.

The languages supported are: en, fr and es.

FormatEnglishFrenchSpanish
defaultSun Sep 25 2016 13:48:02Dim 25 Sep 2016 13:50:52Dom 25-IX-2016 13:52:33
shortDate9/25/1625/9/1625/9/16
mediumDateSep 25, 201625 Sep 201625-IX-2016
longDateSeptember 25, 201625 Septembre 201625 de septiembre de 2016
fullDateSunday, September 25, 2016Dimanche 25 Septembre 2016Domingo 25 de septiembre de 2016
shortTime1:48 PM13:5013:52
mediumTime1:48:02 PM13:50:5213:52:33
longTime1:48:02 PM GMT+020013:50:52 GMT+020013:52:33 GMT+0200
FormatValues
sortableDate2016/09/25
isoDate2016-09-25
isoTime13:48:02
isoDateTime2016-09-25T13:48:02
isoUtcDateTime2016-09-25T11:48:02Z

Contributing

Please report to me any bug you find. And any help is most welcome especialy for internationalization and localization.

1.0.1

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

7 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago