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.
DateConv, add this to your code:
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.
|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|
var str = DateConv.toString(new Date(), 'd/M/yyyy HH:mm:ss.S');
var date = DateConv.toDate('09/11, 2001, 08:46:00', 'MM/dd, yyyy, HH:mm:ss');
DateConv.toDate()returns null when:
DateConv.toDate('33/01/1981', 'dd/MM/yyyy'), where the day of month is obviously invalid
new Date(year, month, day, hour, minute, second, millisecond)to construct Date objects, any date parts that are not specified in the date string, will be replaced by 0 (except the day of month, which will be replaced by 1).
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.
DateConv.toString()will return invalid year in this case.