|
Mailing Lists
|
Home /
Groups /
ColdFusion Community (CF-Community)
Javascript date sorting
Author: Jochem van Dieten
Short Link: http://www.houseoffusion.com/groups/cf-community/thread.cfm/threadid:17191#157883
Matthew Small wrote:
----- Excess quoted text cut - see Original Post for more -----
That almost worked, just needed to get the val() right and switch some substring
positions. Thanx.
Jochem
Author: Jim Davis
Short Link: http://www.houseoffusion.com/groups/cf-community/thread.cfm/threadid:17191#157879
I'm working on this now for something.
Personally I think it's a mistake to treat the dates as strings - first
convert them to dates, then your job will be easier.
It may be as simple as this:
var d=new Date("05/17/2005 01:33 AM")
If the string doesn't parse correctly (if JavaScript doesn't recognize the
date format) a regex could be used to convert it to a date format that
Javascript does recognize.
To actually deal with the objects I'm using my obCollectionOrdered
component. It's available here:
http://www.depressedpress.com/depressedpress/Content/Development/JavaScript/
Extensions/ObCollectionOrdered/Index.cfm
Once I have the dates I you could do a simple sort function and pass it to
the sort method. I actually create a new property of the date object
containing the value of Date.parse() (a numeric representation of the date).
I can then use the "SortByProp()" method of my collection component to sort
the dates (both ascending and descending). It's insanely simple after that
to display the dates (in any format you like) by looping over the
collection.
Works a treat.
Jim Davis
Author: Matthew Small
Short Link: http://www.houseoffusion.com/groups/cf-community/thread.cfm/threadid:17191#157864
You'll need to use a little more processing to change the AM/PM into a
military system.
Something to the effect of:
function DateTimeOrderFormat (dt)
{
hr = dt.substr(14,2);
if(hr == "12"){hr = "00";}
if (dt.substr(11,2) == "PM") {hr = val(hr) + 12;}
return dt.substr(6,4)+dt.substr(0,2)+dt.substr(3,2)+hr+
bb.substr(14,2);
}
- Matt Small
G wrote:
>
> No need to include A's and P's once yer in military time....your sorting
> would then be based on straight 00-24.
But I don't have it in 24 hour time. I have a piece of HTML that I extract
strings from and the strings are formatted like "05/17/2005 01:33 AM".
Jochem
Author: Jochem van Dieten
Short Link: http://www.houseoffusion.com/groups/cf-community/thread.cfm/threadid:17191#157860
G wrote:
>
> No need to include A's and P's once yer in military time....your sorting
> would then be based on straight 00-24.
But I don't have it in 24 hour time. I have a piece of HTML that I extract
strings from and the strings are formatted like "05/17/2005 01:33 AM".
Jochem
Author: G
Short Link: http://www.houseoffusion.com/groups/cf-community/thread.cfm/threadid:17191#157858
Oops, that should be 20051235 of course, not 20051335.
I hate Tuesdays.
>
> For "12:35AM" you'd have: 20050035
> For "12:35PM" you'd have: 20051335
>
> No need to include A's and P's once yer in military time....your sorting
> would then be based on straight 00-24.
>
Author: G
Short Link: http://www.houseoffusion.com/groups/cf-community/thread.cfm/threadid:17191#157857
----- Excess quoted text cut - see Original Post for more -----
Why?
For "12:35AM" you'd have: 20050035
For "12:35PM" you'd have: 20051335
No need to include A's and P's once yer in military time....your sorting
would then be based on straight 00-24.
Author: Jochem van Dieten
Short Link: http://www.houseoffusion.com/groups/cf-community/thread.cfm/threadid:17191#157856
G wrote:
> Can you work with the dates in military time?
I wish I could, that would be easy. But this is for a Greasemonkey script so I
have to work with the format that is available (unless Michael wants to change
the HoF archives to 24 hour times). I could of course add something to produce
20051305A0900 vs 20051305P0900 but that doesn't work quite right with times like
12:35 AM.
Jochem
Author: G
Short Link: http://www.houseoffusion.com/groups/cf-community/thread.cfm/threadid:17191#157855
Can you work with the dates in military time?
>
> How do I get proper AM/PM parsing into this?
>
> Jochem
>
Author: Jochem van Dieten
Short Link: http://www.houseoffusion.com/groups/cf-community/thread.cfm/threadid:17191#157844
I need to sort an array of strings in the format "05/17/2005 01:33 AM" by date
using javascript. For that I need a custom sort function. What I have so far is:
function sortTable()
{
sortfn = mysort;
var newRows = new Array();
for (j=1;j<newIndex.rows.length;j++) newRows[j-1] = newIndex.rows[j];
newRows.sort(mysort);
}
function mysort(a,b)
{
aa = a.cells[2].firstChild.innerHTML;
bb = b.cells[2].firstChild.innerHTML;
dt1 =
aa.substr(6,4)+aa.substr(0,2)+aa.substr(3,2)+aa.substr(11,2)+aa.substr(14,2);
dt2 =
bb.substr(6,4)+bb.substr(0,2)+bb.substr(3,2)+bb.substr(11,2)+bb.substr(14,2);
if (dt1==dt2) return 0;
if (dt1>dt2) return -1;
return 1;
};
How do I get proper AM/PM parsing into this?
Jochem
|
May 17, 2012
|
Latest Fusion Authority Articles
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||