Date parses incorrectly with a parse string where %p comes prior to %I #1155

Open
scags9876 opened this Issue Oct 11, 2012 · 1 comment

Projects

None yet

1 participant

@scags9876

Use a parse string like this: '%p %I', so that a time of "PM 5" will parse correctly, does not work.

Date.defineParser('%p %I'); 
var d=Date.parse('PM 5'); 
d.format('%p %I');

The above returns 'AM 05'.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@scags9876

This is happening because the 'PM' is parsing prior to the hour being set in the date object. At that point, the hour is zero.

Then, when the hour is set, it is set to 5, and never incremented 12 hours as it should be since it's a PM.

One way to fix this (below), is to be sure to process the %p key last. Patch follows, (sorry for the text patch, issues with my git right now)

@@ -1277,7 +1277,15 @@
                        if ('d' in bits) handle.call(date, 'd', 1);
                        if ('m' in bits || bits.b || bits.B) handle.call(date, 'm', 1);

-                       for (var key in bits) handle.call(date, key, bits[key]);
+                       for (var key in bits) {
+                            if(key=='p') {continue;}
+                            handle.call(date, key, bits[key]);
+                        }
+
+                        //if am/pm is in there, need to do it last
+                        if('p' in bits) {
+                            handle.call(date, 'p', bits['p']);
+                        }
                        return date;
                }
        };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment