Update of DP_DateExtensions to Correct Parsing Error

A slight update to my JavaScript date extensions, DP_DateExtensions, is available for download.  This release addresses another issue related to paring of “AM” and “PM” indicators.  The gist of the problem was that the parser was transversing Noon and Midnight.

I had two bugs.  When I encountered a PM indicator I was simply adding 12 to the “hours” value if it were less than 13.  Of course 12 noon plus 12 hours is midnight the next day.  I needed to check for “less than 12” not “less than 13”.  However I also wasn’t taking any action if the indicator was AM – which left 12 AM as “12”, or noon, in the resulting 24 hour clock representation.  If the indicator were AM I needed to check if the hours were 12 and set them to zero if so.

For those interested the actual changed snippet is below (lines 278-292 in the component).  Note that in this snippet “DF[3]” is the hours as pulled from the parsed string and “DF[7]” is the am/pm indicator (DF, by the way, is “Date Fragment”):

// Set AM/PM, if present (Hours must be present and less than 12 for it to matter)
if ( DF[7] != null && DF[3] != null ) {
var CurAP = DF[7].substring(0,1).toLowerCase();
if ( CurAP == "p" ) {
// Add 12 if PM and hours is less than 12 (use parseInt to ensure that values with leading zeroes are not mistaken for strings)
if ( DF[3] < 12 ) {
DF[3] = parseInt(DF[3], 10) + 12;
};
} else {
// 12 AM is zero, not 12
if ( DF[3] == 12 ) {
DF[3] = 0;
};
};
};

Yet another silly little error that proves that a) I need to improve my test plans and b) date math continues to truly suck.

Leave a Reply