JsSimpleDateFormat   v1.0
Author: AT Mulyana                          9 May 2008


Table of Contents

Description

JsSimpleDateFormat is for formatting Date object to be a string as the pattern we define and also for parsing a string to be a Date object based on the defined pattern. JsSimpleDateFormat works similar with Java SimpleDateFormat class which uses the same pattern letters with similar rules. If you have been familiar with Java SimpleDateFormat class, you should be able to use JsSimpleDateFormat easily.

This library contains two main classes, those are JsSimpleDateFormat and JsDateFormatSymbols. (We still use Class term, even if in the fact, Javascript doesn't have class in its grammar).

Date and Time Patterns

As mentioned above, JsSimpleDateFormat uses the same pattern letters with similar rules as used by Java SimpleDateFormat class. To make the same comprehension, the explanation about the pattern letters here is copied from Java (TM) 2 Platform Standard Edition 5.0 API Specification with some modifications and additions. Java (TM) 2 Platform Standard Edition 5.0 API Specification is copyrighted by Sun Microsystems, Inc.

Note: There are some differences between JsSimpleDateFormat and Java SimpleDateFormat class: The list of the pattern letters:
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
Pattern letters are usually repeated, as their number determines the exact presentation:

Class JsSimpleDateFormat

Constructor

Property

Method

Class JsDateFormatSymbols

The objects of this class is to specify the name of days, months and the other keywords which are usually shown in date time information. The JsSimpleDateFormat object uses an object of JsDateFormatSymbols to get all keywords needed either for formatting or for parsing. All JsSimpleDateFormat objects always have a JsDateFormatSymbols object associated to it. When JsSimpleDateFormat object is created, it will also create JsDateFormatSymbols object based on the parameters passed to constructor. This JsDateFormatSymbols object can be obtained by getDateFormatSymbols method. You may also create a new JsDateFormatSymbols object and set it via setDateFormatSymbols method. In other words, you can set the new keywords according to your languange.

Below is an example how to change the keywords:
		var oDf = new JsSimpleDateFormat("MMM d, yyyy");
		var oSymbols = new JsDateFormatSymbols();
		oSymbols.setAmPmStrings(['AM','PM']);
		oSymbols.setEras(['AD','BC']);
		oSymbols.setMonths(['January','February','March','April','May','June','July','August','September','October','November','December']);
		oSymbols.setShortMonths(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']);
		oSymbols.setShortWeekdays(['Sun','Mon','Tue','Wed','Thu','Fri','Sat']);
		oSymbols.setWeekdays(['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']);
		oDf.setDateFormatSymbols(oSymbols);
		alert(oDf.format(new Date()));
	

Of course, you should change the words in the example above to be the appropriate words according to your language.

Another Trick
If you want set a code for your language like "en" for English and can pass this code whenever creating JsSimpleDateFormat object, do these steps. Specify the code for you language, suppose you want the code "xx". It should be two letters as specified in http://www.loc.gov/standards/iso639-2/php/English_list.php, but you may choose what you like. Nevermind if for your own. Create a JavaScript file and write the code like the below one inside this JavaScript file.

		JsDateFormatSymbols.__symbols__.xx = {
			amPmStrings: ['AM','PM'],
			eras: ['AD','BC'],
			months: ['January','February','March','April','May','June','July','August','September','October','November','December'],
			shortMonths: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
			shortWeekdays: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
			weekdays: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
		};
	

Alter the words in the example above to be the appropriate words according to your language. Insert that JavaScript file in your HTML document after inserting JsSimpleDateFormat.js.

Method

Bugs Report

You may send bug reports to [email protected].