Skip to content Skip to sidebar Skip to footer

Sorting Utc Dates In Javascript

EDIT 4/16/2012: I solved the issue of getting the timezone abbreviated into a letter format, had to download a third party sorting method and add a few things to get the desired re

Solution 1:

UPDATE:

I was looking at another question, and someone created a knockout grid addon: https://github.com/ericmbarnard/KoGrid

I bet this might help you out :-)

---OLD ANSWER for nostalgia----

There are some great helper functions in the Underscore library, one of them being sort:

http://documentcloud.github.com/underscore/#sortBy

sortBy_.sortBy(list, iterator, [context]) Returns a sorted copy of list, ranked in ascending order by the results of running each value through iterator. Iterator may also be the string name of the property to sort by (eg. length).

_.sortBy([1, 2, 3, 4, 5, 6], function(num){ returnMath.sin(num); });
=> [5, 4, 6, 3, 1, 2]

I'd give this a shot, along with creating a better model for your dates. It sounds like you need to store a property which is a unique point in time, along with a text value for the user.

Solution 2:

Well, to get numeric values for your date objects, sort by pDate.valueOf(). This will give you the # of millisecond since epoch.

However, there is an issue inside of your sort function, but I'm not sure what it is supposed to do. You can't walk an object inside of a sort function and return values like that.

Post a Comment for "Sorting Utc Dates In Javascript"