DateConv - Usage / Demo

DateConv is a javascript class which converts Date objects into String objects, or the other way around.

I decided to wrote this simple tool because I found there are no similar tools available. All I need is a simple converter that supports simple formats, and this is what DateConv turned out to be.

You can copy / modify the code at your will.

Download

To use DateConv, add this to your code:

<script type="text/javascript" src="dateconv-min.js"></script>
		

  1. Date Formats
  2. DateConv.toString(date, format)
  3. DateConv.toDate(str, format)
  4. Known Glitches

Date Formats

DateConv uses a pattern format similar to Java's SimpleDateFormat class, but doesn't exactly follow SDF's rules. DateConv's patterns are much simpler.

Format Stands For Remarks
yyyy 4 digits year
yy 2 digits year toDate() doesn't support this format
MM Month in year with zero padding, 01 - 12
M Month in year, 1 - 12 toDate() doesn't support this format
dd Day in month with zero padding, 01 - 31
d Day in month, 1 - 31 toDate() doesn't support this format
HH Hour in day with zero padding, 00 - 23
H Hour in day, 0 - 23 toDate() doesn't support this format
mm Minute in hour with zero padding, 00 - 59
m Minute in hour, 0 - 59 toDate() doesn't support this format
ss Second in minute with zero padding, 00 - 59
s Second in minute, 0 - 59 toDate() doesn't support this format
SSS Millisecond in second with zero padding, 000 - 999
S Millisecond in second, 0 - 999 toDate() doesn't support this format

DateConv.toString(date, format)

Usage

var str = DateConv.toString(new Date(), 'd/M/yyyy HH:mm:ss.S');
		

DateConv.toDate(str, format)

Usage

var date = DateConv.toDate('09/11, 2001, 08:46:00', 'MM/dd, yyyy, HH:mm:ss');
		

Notes


Known Glitches

  1. Dates like 2008/02/31 are valid because when checking the day of month, DateConv.toDate() only checks if it's in the 1 - 31 range. Thus 2008/02/31 will actually be translated as 2008/03/02, so be careful.
  2. Dates precede 0 A.D. are NOT SUPPORTED, DateConv.toString() will return invalid year in this case.