JavaScripTools Manual

Parsers

Working with the DateParser

The DateParser is a class that can convert strings to dates and dates to strings using a mask. The mask is a string containing the following charactes:

d: Day M: month y: year
h: 12 hour H: 24 hour m: minute
s: second S: millisecond
a: am / pm A: AM / PM
/. -: Separators

The constructor parameters are:

For more details about customizing those default values, check out this chapter.

If the mask has to change, just setting it will make no effect. To do so, you must call the setMask(newMask) method.

Here are some examples (remember that, the JavaScript Date object, the month starts on 0):

//The default value for enforceLength is true
var parser = new DateParser("yyyy-MM-dd");
parser.format(new Date(2006, 9, 2)) -> "2006-10-02"
parser.parse("2000-12-31") -> Dec 31, 2000
parser.parse("06-7-3") -> null (enforceLength is enabled)
parser.parse("ABC") -> null

var parser2 = new DateParser("d/M/yy H:m:s");
parser2.format(new Date(2006, 3, 7, 9, 30, 02)) -> "7/3/06 9:30:2"



Previous:
Working with the NumberParser
Table of Contents Next:
Using the CustomParser