House of Fusion
Home of the ColdFusion Community
Hostmysite VPS Hosting

Search cf-community

August 21, 2008

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
           1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31             

Subscribe Now
Fusion Authority Quarterly Update - ColdFusion 8 Special Edition

For ColdFusion hosting try HostMySite.com.
Search over 2,500 ColdFusion resources here  >>>      
Home /  Groups /  ColdFusion Community (CF-Community)

Javascript date sorting

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Can you work with the dates in military time?
** Private **
05/17/05 08:14 A
G wrote:
** Private **
05/17/05 08:34 A
>
** Private **
05/17/05 08:46 A
G wrote:
** Private **
05/17/05 08:52 A
Matthew Small wrote:
** Private **
05/17/05 10:31 A
I'm working on this now for something.
** Private **
05/17/05 10:21 A

05/17/2005 06:47 AM
Author:
** Private **

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

05/17/2005 08:14 AM
Author:
** Private **

Can you work with the dates in military time? > > How do I get proper AM/PM parsing into this? > > Jochem >

05/17/2005 08:34 AM
Author:
** Private **

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

05/17/2005 08:46 AM
Author:
** Private **

> 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.

05/17/2005 08:49 AM
Author:
** Private **

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. >

05/17/2005 08:52 AM
Author:
** Private **

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

05/17/2005 09:16 AM
Author:
** Private **

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

05/17/2005 10:31 AM
Author:
** Private **

Matthew Small wrote: That almost worked, just needed to get the val() right and switch some substring positions. Thanx. Jochem

05/17/2005 10:21 AM
Author:
** Private **

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


<< Previous Thread Today's Threads Next Thread >>

Mailing Lists