Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Y-m-d #34

Merged
merged 6 commits into from
Feb 23, 2014
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea

lib-cov
*.seed
*.log
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
var instances = [], matchers = [];
matchers.push(/^[0-9]*$/.source);
matchers.push(/([0-9]{1,2}\/){2}[0-9]{4}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
matchers.push(/[0-9]{4}(\/[0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
matchers.push(/[0-9]{4}([\/\-][0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At line #29 you could just add a conditional statement in parseDate function to replace dashes to slashes, simple like:

if(String(dateString).match(matchers)) {
    // If looks like a millisecond value cast to number before 
    // final casting (Thanks to @msigley)
    if(String(dateString).match(/^[0-9]*$/)) {
        dateString = Number(dateString);
    }
    // Replace dashes to slashes
    if(String(dateString).match(/^[0-9]*$/)) {
        dateString = dateString.replace(/\-/g, '/');
    }
    return new Date(dateString);
}

matchers = new RegExp(matchers.join("|"));
function parseDateString(dateString) {
if (dateString instanceof Date) {
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.countdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
matchers.push(/^[0-9]*$/.source);
// Month/Day/Year [hours:minutes:seconds]
matchers.push(/([0-9]{1,2}\/){2}[0-9]{4}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
// Year/Day/Month [hours:minutes:seconds]
matchers.push(/[0-9]{4}(\/[0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
// Year/Day/Month [hours:minutes:seconds] and Year-Day-Month [hours:minutes:seconds]
matchers.push(/[0-9]{4}([\/\-][0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
// Cast the matchers to a regular expression object
matchers = new RegExp(matchers.join("|"));
// Parse a Date formatted has String to a native object
Expand Down
7 changes: 7 additions & 0 deletions test/countdown_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ asyncTest('cast YYYY/MM/DD hh:mm:ss', 1, function() {
});
});

asyncTest('cast YYYY-MM-DD hh:mm:ss', 1, function() {
$dom.countdown('2020-10-20 12:34:56').on('update.countdown', function(event) {
ok(event.finalDate.toString().match(/Oct 20 2020 12:34:56/));
start();
});
});

asyncTest('cast YYYY/MM/DD', 1, function() {
$dom.countdown('2020/10/20').on('update.countdown', function(event) {
ok(event.finalDate.toString().match(/Oct 20 2020 00:00:00/));
Expand Down