pax.date

This is the date parsing and manipulation library; the date formatting methods in this script are from the awesome script by Baron Schwartz, originally published here:

http://www.xaprb.com/blog/2005/12/12/javascript-closures-for-runtime-efficiency/

Note: whilst the standard way for PAX library scripts is to not override the prototype of any built-in objects, the date object is an exception.  Some portions have been modified slightly from the original script.

Authors

Baron Schwartz http://www.xaprb.com/, Mikkel Bergmann http://www.pointful.com,

Summary
pax.dateThis is the date parsing and manipulation library; the date formatting methods in this script are from the awesome script by Baron Schwartz, originally published here:
Functions and Properties
date.dateFormatSets the expected format for a date object.
pax.date.guessCfgControls how the date guesser handles matches and keywords, you can override this with a different langauge, English (Australian) is default.
date.guessPopulates the date obj based on the string in the input

Functions and Properties

date.dateFormat

Sets the expected format for a date object.  The format is like the PHP date syntax.

Sample date: 'Wed Jan 10 2007 15:05:01 GMT-0600 (Central Standard Time)'

Format  Output      Description
------  ----------  --------------------------------------------------------------
  d      10         Day of the month, 2 digits with leading zeros
  D      Wed        A textual representation of a day, three letters
  j      10         Day of the month without leading zeros
  l      Wednesday  A full textual representation of the day of the week
  S      th         English ordinal day of month suffix, 2 chars (use with j)
  w      3          Numeric representation of the day of the week
  z      9          The julian date, or day of the year (0-365)
  W      01         ISO-8601 2-digit week number of year, weeks starting on Monday (00-52)
  F      January    A full textual representation of the month
  m      01         Numeric representation of a month, with leading zeros
  M      Jan        Month name abbreviation, three letters
  n      1          Numeric representation of a month, without leading zeros
  t      31         Number of days in the given month
  L      0          Whether it's a leap year (1 if it is a leap year, else 0)
  Y      2007       A full numeric representation of a year, 4 digits
  y      07         A two digit representation of a year
  a      pm         Lowercase Ante meridiem and Post meridiem
  A      PM         Uppercase Ante meridiem and Post meridiem
  g      3          12-hour format of an hour without leading zeros
  G      15         24-hour format of an hour without leading zeros
  h      03         12-hour format of an hour with leading zeros
  H      15         24-hour format of an hour with leading zeros
  i      05         Minutes with leading zeros
  s      01         Seconds, with leading zeros
  O      -0600      Difference to Greenwich time (GMT) in hours
  T      CST        Timezone setting of the machine running the code
  Z      -21600     Timezone offset in seconds (negative if west of UTC, positive if east)

Example

<div id='date.format.example1'></div>
[:.
    var myDate = new Date();
    $('date.format.example1').innerHTML = myDate.dateFormat( 'D M Y' );
:]

This will display todays date as “DDD MMM YYYY”

pax.date.guessCfg

Controls how the date guesser handles matches and keywords, you can override this with a different langauge, English (Australian) is default.

  • seperators - Seperators for the date patterns - use ‘*’ in a datePattern, to match a seperator
  • datePatterns - What dates to try and match; order is important; have non-seperated items last.  Note that these are php-style date patterns
  • keyWord - Object with keyword as key, list of allowed matches as value

date.guess

Populates the date obj based on the string in the input

Parameters

inputstring to guess from.  There are various commands and entities that are valid:
extraDateFormatsdate formats you may want to additionally accept

Input format

[date] [command] [entitity]

Commands

  • ”next [entity]” - finds the next occurance of [entity]
  • ”last [entity]” - finds the previous occurance of [entity]
  • ”+ [number] [entity]” - finds the [number] next occurance of [entity]
  • ”- [number] [entity]” - finds the [number] previous occurance of [entity]

Entities

  • today - gives the current date (‘t’ also works)
  • tomorrow - gives tomorrows date (‘tom’ also works)
  • yesterday - gives yesterdays date (‘yes’ also works)
  • weekdays - both short and long weekdays, eg: ‘mon’, or ‘Monday’ both work
  • month names - both short and long month names, eg: ‘aug’, or ‘August’ both work
  • day - ‘d’ and ‘days’ also work
  • week - ‘w’ and ‘weeks’ also work
  • month - ‘m’ and ‘months’ also work
  • year - ‘y’ and ‘years’ also work

Returns

Either a date object populated with the guessed date, or null, if we can’t guess the date

Example

var myDate = Date.guess( 'next friday' );

myDate now contains the date object of next friday

var myDate = Date.guess( 't +2weeks' );

myDate now contains the date in 2 weeks time

var myDate = Date.guess( 'tomorrow +1month' );

myDate now contains the date object of 1 month from tomorrow

var myDate = Date.guess( 'last sunday' );

myDate now contains the date object of last sunday

var myDate = Date.guess( '01-01-2001 +2years' );

myDate now contains the date of the 1st of January 2003

Close