Page 1 of 1

Javascript Date() on _createdAt field returns Invalid Date

Posted: Sat Apr 27, 2013 2:54 am
by Christine Stringfield

Hello,

I have some Javascript I am trying to use to convert the _createdAt timestamp to something more readable. However if I take the value and pass it into new Date(value) it returns "Invalid Date". I know that the value (ie 2013-04-25 02:02:35.655) is getting passed into the function.

Here is my code:

var date = new Date(value);
alert(date);
var hour = date.getHours()-7;
date.setHours(hour);
var diff = (((new Date()).getTime() - date.getTime()) / 1000);
var day_diff = Math.floor(diff / 86400);

if ( isNaN(day_diff) day_diff < 0 day_diff = 31 )
{
return;
}
else
{
return day_diff == 0 && (
diff < 60 && "just now"
diff < 120 && "1 minute ago"
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago"
diff < 7200 && "1 hour ago"
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago")
day_diff == 1 && "Yesterday"
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}


Javascript Date() on _createdAt field returns Invalid Date

Posted: Sat Apr 27, 2013 3:03 am
by Christine Stringfield

I noticed that this works fine in Chrome but not in Firefox. Do you have any suggestions for getting the _createdAt date into a format that will be compatible with the Date() function?


Javascript Date() on _createdAt field returns Invalid Date

Posted: Sat Apr 27, 2013 3:06 am
by maxkatz

Check/debug where exactly (what line) it fails in Firefox. It's interesting.. but looks like there are differences in implementation between Chrome and Firefox. You could also visit Firefox dev. site to see if there is a solution to this.