Skip to content

dateFormat fails if no time present (Partial Fix inside) #96

@Joshdw

Description

@Joshdw

So I'm working on a project where I was using dateFormat for formatting various kinds of dates client-sided. It worked great until I noticed dates that didn't have time ("2016-06-11") would return an error (IndexOf is undefined) and not format the date. After a quick snoop around I figured it was because the calculations didn't return a Number, so it failed to run the parseTime function.

Here's the problematic code (Line 180 in current dist):

subValues = values[0].split('');
parsedDate.year       = subValues[0] + subValues[1] + subValues[2] + subValues[3];
parsedDate.month      = subValues[5] + subValues[6];
parsedDate.dayOfMonth = subValues[8] + subValues[9];
parsedDate.time       = parseTime(subValues[13] + subValues[14] + subValues[15] + subValues[16] + subValues[17] + subValues[18] + subValues[19] + subValues[20]);
break;

Add a simple line to fix it:

subValues = values[0].split('');
parsedDate.year       = subValues[0] + subValues[1] + subValues[2] + subValues[3];
parsedDate.month      = subValues[5] + subValues[6];
parsedDate.dayOfMonth = subValues[8] + subValues[9];
if(!isNaN(subValues[13] + subValues[14] + subValues[15] + subValues[16] + subValues[17] + subValues[18] + subValues[19] + subValues[20]))
parsedDate.time       = parseTime(subValues[13] + subValues[14] + subValues[15] + subValues[16] + subValues[17] + subValues[18] + subValues[19] + subValues[20]);
break;

After that change, you can now use date-only formats with the plugin. Can someone confirm this was an issue or if time-less dates are not intended.

The only issue I am getting now is reverse-year formats, where the year is at the end of the format such as "11-05-2016" would return an invalid date. Any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions