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

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

Open
Joshdw opened this issue Jul 11, 2016 · 3 comments
Open

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

Joshdw opened this issue Jul 11, 2016 · 3 comments

Comments

@Joshdw
Copy link

Joshdw commented Jul 11, 2016

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?

@Joshdw Joshdw changed the title dateFormat fails if no time present (Fix inside) dateFormat fails if no time present (Partial Fix inside) Jul 11, 2016
@phstc
Copy link
Owner

phstc commented Jul 11, 2016

@Joshdw thanks for researching on this 🍻

Could you add some specific examples of calls that fail and what they should return?

So we can add those here. That will help to understand better the issues and expectations.

@Joshdw
Copy link
Author

Joshdw commented Jul 11, 2016

Sure no problemo!

The format I noticed this with was the default sql date format (yyyy-mm-dd). While I was testing I always got this error:
TypeError: time.indexOf is not a function

I then added the !isNaN line and did some tests. Here are the results:

Code:

console.log("2016-06-11 => " + $.format.date("2016-06-11", "dd MMMM yyyy"));
console.log("2016.06.11 => " + $.format.date("2016.06.11", "dd MMMM yyyy"));
console.log("2016 06 11 => " + $.format.date("2016 06 11", "dd MMMM yyyy"));
console.log("2016-11-06 => " + $.format.date("2016-11-06", "dd MMMM yyyy"));
console.log("2016.11.06 => " + $.format.date("2016.11.06", "dd MMMM yyyy"));
console.log("2016 11 06 => " + $.format.date("2016 11 06", "dd MMMM yyyy"));
console.log("11-06-2016 => " + $.format.date("11-06-2016", "dd MMMM yyyy"));
console.log("11.06.2016 => " + $.format.date("11.06.2016", "dd MMMM yyyy"));
console.log("11 06 2016 => " + $.format.date("11 06 2016", "dd MMMM yyyy"));
console.log("06-11-2016 => " + $.format.date("06-11-2016", "dd MMMM yyyy"));
console.log("06.11.2016 => " + $.format.date("06.11.2016", "dd MMMM yyyy"));
console.log("06 11 2016 => " + $.format.date("06 11 2016", "dd MMMM yyyy"));

Output:

2016-06-11 => 11 June 2016
2016.06.11 => 11 June 2016
2016 06 11 => 2016 06 11
2016-11-06 => 06 November 2016
2016.11.06 => 06 November 2016
2016 11 06 => 2016 11 06
11-06-2016 => 16 -2 11-0
11.06.2016 => 16 .2 11.0
11 06 2016 => 11 06 2016 
06-11-2016 => 16 -2 06-1
06.11.2016 => 16 .2 06.1
06 11 2016 => 06 11 2016

At first I tried appending "00:00:00.000" to the Date strings before formatting them, but that seemed to be unreliable as it failed with "undefined" often. But yeah, it works right now for what I need it for (yyyy-mm-dd!). Hope that could help a little bit. Sorry if that wasn't legible in some way, I'm off to bed now :)

@Joshdw
Copy link
Author

Joshdw commented Jul 12, 2016

Some more testing (day/month above 12 to differenciate formats):

2016-06-14 => 14 June 2016
2016.06.14 => 14 June 2016
2016 06 14 => 2016 06 14
2016-14-06 => 06 14 2016
2016.14.06 => 06 14 2016
2016 14 06 => 2016 14 06
14-06-2016 => 16 -2 14-0
14.06.2016 => 16 .2 14.0
14 06 2016 => 14 06 2016 
06-14-2016 => 16 -2 06-1
06.14.2016 => 16 .2 06.1
06 14 2016 => 06 14 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants