You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@flex.apache.org by core000 <ka...@gmail.com> on 2016/04/11 16:39:04 UTC

Determining Maximum Date from Array

I trying to determine the max date from an ArrayCollection to be used in a
DateField component.     Below is my code.    But it's not working.   The
return variable sends a random date.

public var ac1:Array;
			
			public function getMax(ac:ArrayCollection):void
			{
                                 ac1 = ac.toArray();
				var mxm:Date = convertSQLDate(ac1[0].eventDate);
				for (var i:int=0; i<ac1.length; i++) {
					var tempDate:Date = convertSQLDate(ac1[i].eventDate);
					if (tempDate>mxm) {
						mxm = tempDate;
					}
				}
			 MAX_DATE = mxm;
			}


public static function convertSQLDate(dateString:String):Date
			{
				if( dateString == null ) return null;

				var datePattern : RegExp = /(\d{4})-(\d+)-(\d+)( (\d+):(\d+):(\d+))?/;
				var result : Object = datePattern.exec( dateString );
				
				if( result[ 4 ] != null ) return new Date( result[1], result[2] - 1,
result[3], result[ 5 ], result[ 6 ], result[ 7 ] );
				else return new Date( result[1], result[2] - 1, result[3] );            
				
			}


Note: The date in the ArrayCollection is set to String format.



--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Determining-Maximum-Date-from-Array-tp12434.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Determining Maximum Date from Array

Posted by Evyatar Ben Halevi-Arbib <ev...@gmail.com>.
A general tip for converting an entire array -
Use the array.map function, which gets a conversion function as input.

A typical mapping function would look something like this -

private function createViewItem(inputItem:InputType, index:int,
arr:Array):OutputType
{
return new OutputType(inputItem.constructorParam);
}

On Mon, Apr 11, 2016 at 11:29 PM, core000 <ka...@gmail.com> wrote:

> Thanks Piotrz for the quick response.    The date in the ArrayCollection is
> set to String format, not a Date format.   So the SORT function does not
> work.    Also some items in the ArrayCollection field might be NULL.
>
> Is there a way to convert all objects in an ArrayColelction (that comes via
> JSON form DB) to Date object?
>
> Thanks,
>
>
>
> --
> View this message in context:
> http://apache-flex-users.2333346.n4.nabble.com/Determining-Maximum-Date-from-Array-tp12434p12436.html
> Sent from the Apache Flex Users mailing list archive at Nabble.com.
>

Re: Determining Maximum Date from Array

Posted by piotrz <pi...@gmail.com>.
It depends how string in DB is looks like. Post example of such string. 

You can also try to use "parseDateString" functionin DateFormatter and see
what happens.


[1]http://help.adobe.com/pl_PL/FlashPlatform/reference/actionscript/3/mx/formatters/DateFormatter.html#parseDateString()

Piotr



-----
Apache Flex PMC
piotrzarzycki21@gmail.com
--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Determining-Maximum-Date-from-Array-tp12434p12437.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Determining Maximum Date from Array

Posted by core000 <ka...@gmail.com>.
Thanks Piotrz for the quick response.    The date in the ArrayCollection is
set to String format, not a Date format.   So the SORT function does not
work.    Also some items in the ArrayCollection field might be NULL.

Is there a way to convert all objects in an ArrayColelction (that comes via
JSON form DB) to Date object?

Thanks,



--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Determining-Maximum-Date-from-Array-tp12434p12436.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Determining Maximum Date from Array

Posted by piotrz <pi...@gmail.com>.
Hi,

Since in your ArrayCollection you have Date objects you can use "time" [1]
property to determine max date.

Simply use "Sort" function in ArrayCollection to sort dates descending using
"time" property as an sort field. Once you sort your collection - first
element in it will be your max date. :)

[1]
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Date.html#time

Piotr



-----
Apache Flex PMC
piotrzarzycki21@gmail.com
--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Determining-Maximum-Date-from-Array-tp12434p12435.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Determining Maximum Date from Array

Posted by piotrz <pi...@gmail.com>.
How convertSQLDate looks like ? Can you paste full code.

Piotr



-----
Apache Flex PMC
piotrzarzycki21@gmail.com
--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Determining-Maximum-Date-from-Array-tp12434p12713.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Determining Maximum Date from Array

Posted by core000 <ka...@gmail.com>.
Here is the data string coming from DB via JSON:

"eventDate":"2010-04-28"



--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Determining-Maximum-Date-from-Array-tp12434p12712.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Determining Maximum Date from Array

Posted by core000 <ka...@gmail.com>.
Using the Time function works when running application locally.   See my code
below.   However when I publish to my server, the application freezes.  
What is the difference between running the code through Flash Builder and
running it from the server (Bin Release folder)?   And yes, the
convertSQLDate() function is working.

			public function getMin(ac:ArrayCollection):void
			{
						
				ac2 = ac.toArray();
				
				var mxm2:Date = new Date();
				var tnum:Number = mxm2.time;

				for (var i2:int=0; i2<ac.length; i2++) {
					
					if(ac2[i2].eventDate){
						var tempDate2:Number = convertSQLDate(ac2[i2].eventDate).time;
						trace("date is " + tempDate2);
					}
			         }
			} 





--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Determining-Maximum-Date-from-Array-tp12434p12711.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Determining Maximum Date from Array

Posted by Nemi <ne...@gmail.com>.
Did you try "if (tempDate.time > mxm.time)" ?
Also, you should check if the first one is parsed ok! So check mxm after: 
var mxm:Date = convertSQLDate(ac1[0].eventDate);



--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Determining-Maximum-Date-from-Array-tp12434p12594.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.