I'm receiving a date from a server in milliseconds since 1-1-1970. I then use the DateFormatter to print the date to the screen. However, Flex adds timedifference and thus it displays a different time than what I got from the server. I've fixed this by changing the date before printing to screen. But I think that's a bad solution because the date object doesn't hold the correct date.
Does anyone know how to use the dateFormatter to print the date, ignoring the timezone?
this is how I did it:
function getDateString(value:Date):String
{
var millisecondsPerMinute:int = 1000*60;
var newDate:Date = new Date(value.time - (millisecondsPerMinute*value.timezoneOffset));
var dateFormatter:DateFormatter = new DateFormatter();
dateFormatter.formatString = "EEEE DD-MM-YYYY LL:MM AA";
return dateFormatter.format(newDate);
}
Maybe there is something I'm missing but this seems to work for me.
<?xml version="1.0"?>
<!-- formatters\FormatterDateField.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<!-- Declare a DateFormatter and define formatting parameters.-->
<mx:DateFormatter id="dateFormatter"
formatString="EEEE DD-MM-YYYY LL:NN:SS AA"/>
<mx:Label text="Millis (1220836618601 == Monday 08-09-2008 01:16:58 AM):"/>
<mx:TextInput id="dob" text="1220836618601"/>
<mx:Label text="Formatted date UTC: "/>
<mx:TextInput id="formattedDate"
text=""
editable="false"/>
<mx:Label text="Formatted date local: "/>
<mx:TextInput id="formattedDateLoc"
text=""
editable="false"/>
<!-- Format and update the date.-->
<mx:Button label="Format Input"
click="
var d :Date = new Date(parseInt(dob.text));
formattedDate.text=dateFormatter.format(d.toUTCString());
formattedDateLoc.text=dateFormatter.format(d);
"/>
</mx:Application>
Suggesting that instead of passing the date object (which is timezone dependant) into the dateFormatter, pass in the date object's UTC String instead. I didn't find anything that would suggest that the DateFormatter does anything to the timezone, so there shouldn't be any need to try to compensate for the timezone, especially when the date object already provides a method for getting the UTC.
function getDateString(value:Date):String
{
var dateFormatter:DateFormatter = new DateFormatter();
dateFormatter.formatString = "EEEE DD-MM-YYYY LL:MM AA";
return dateFormatter.format(value.toUTCString());
}
In Flex Hero 4.5 you can use the new Spark DateTimeFormatter:
<s:DateTimeFormatter dateTimePattern="HH':'mm':'ss" id="dateFormatterUTC" useUTC="true" />
<s:Label text="{dateFormatterUTC.format(new Date())}" />
The most simple of fixes is to have as many objects as you can (and properties of objects) be strings. The timezoneOffset solution works fine, but the timezoneOffset for many US cities changes twice during the year. The best rule -- everything is a string.
Related
When I am trying to convert bytes to characters, flex stops conversion if it encounters unicoder number 0 (NUL). Why is it so? Flex is able to convert 1-256 unicode numbers except 0.
In the following example, Alert window does NOT display any text, because parameters started with 0 while forming the string message from unicode numbers.
<?xml version="1.0" encoding="utf-8"?>
<s:Application name="Alert" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="init();">
<s:controlBarContent>
<s:Button id="btn"
label="Show alert"
click="init();"/>
</s:controlBarContent>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
protected function init():void {
// if string message value is String.fromCharCode(78,0);, then Alert displays as N
//Here, since message starts with unicode character 0, Alert displays nothing.
//Flex string is getting stopped if it encounters unicode 0, why is it so?
//but flex string supports other contorl ascii characters except NUL (0)
var message:String=String.fromCharCode(0, 78, 1);
Alert.show(message, "", Alert.YES | Alert.NO | Alert.OK | Alert.CANCEL);
}
]]>
</fx:Script>
</s:Application>
I am not sure why flex is not able to convert unicode 0 character?
Temporarily, I am converting them to 32 (empty space) if it is 0.
Thanks in advance.
Good question!. I try it on my Flash Builder 4.6.
As i Know is String.fromCharCode(num),num depend from keycode
if your get the num begin at 1~Max and it not include in keycode list,
it will return whitespace. But if the num is 0,fromCharCode will return undefined ref.
But you cloud get string length from this string array:
{undefined,h}.length = 2
{undefined,h} make to string = undefined. remember it as X.
i will show you the Math
h+X = h+undefined.
you could try this:
var str:String = String.fromCharCode(0,104);
var str1:String = String.fromCharCode(1,104);
var str2:String = String.fromCharCode(104,0,104);
Alert.show(str);
Alert.show(str.length.toString());
Alert.show(str1);
Alert.show(str1.length.toString());
Alert.show(str2);
Alert.show(str2.length.toString());
You will see str2 show you something different.
{h,undefined,h}.length = 3
for each this for string ref will be:
h+X = h+undefined.
I Guess it's reason is String.fromCharCode Function don't catch
if(codenum == 0){return whitespace}
It may use as switch or for or others way begin the code num at 1.
C strings are terminated by a null byte \0 character.
Similar to C, I believe ActionScript interprets this as a null terminated string.
Because 0 byte is not actually a string char. If you are working with binary data keep it in ByteArray
I am not able to get milliseconds with DateTimeFormatter. I lost a few hair to this already:
<fx:Declarations>
<s:DateTimeFormatter id="dtf"
dateTimePattern="{pattern.text}"
errorText="Invalid input value"/>
</fx:Declarations>
<s:HGroup>
<s:TextInput id="pattern" />
<s:Label text="{dtf.format( new Date())}" />
</s:HGroup>
When I type "y-MM-dd HH:mm:ss.SSS#" in {pattern} , I am not seeing anything between the dot and the pound sign. Does SSS only work for parsing?
Well, that's my solutions then:
<fx:Declarations>
<s:DateTimeFormatter id="dtf"
dateTimePattern="{pattern.text}"
errorText="Invalid input value"/>
</fx:Declarations>
<s:HGroup includeIn="start" bottom="5" left="5">
<s:TextInput id="pattern" />
<s:Label text="{dtf.format( new Date()) + (new Date().milliseconds + 1000).toString().substr(1)}" />
</s:HGroup>
And the pattern would be "y-MM-dd HH:mm:ss."
Try using the mx DateFormatter formatter rather than spark DateTimeFormatter.
The format strings for date formatting in DateFormatter may are slightly different than other time/date formatters but milliseconds (Q) is supported and does work.
With Apache Flex 4.10 (and above) this code:
protected function traceDate():void
{
var df:DateFormatter = new DateFormatter()
df.formatString="YYYY-MM-DD HH:NN:SS.QQQQ#";
trace(df.format(new Date()));
}
Produces a time date string like so:
2013-11-30 09:40:11.0528#
End the date string with "." or ":"
example if the date string is "2018-04-19 16:46:49.766"
then make it "2018-04-19 16:46:49.766" + "."
or
"2018-04-19 16:46:49.766" + ":"
I'm trying to learn Flex, I setup a simple Air application with PHP server as dataserice...
In my php class there is a function counttotal that return a simple int value.
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:demologicaclass="services.demologicaclass.*"
width="682" height="397" showStatusBar="false" initialize="init()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
protected var count:int = 0;
protected function init():void
{
counttotalResult.token = logicaservice.counttotal();
count = counttotalResult.lastResult as int;
}
protected function get_count():void
{
Alert.show(count as String);
}
.....
.....
<s:Label id="countitems" left="10" bottom="39" width="221" height="21"
fontSize="20"
fontWeight="bold" text="{counttotalResult.lastResult as String}"/>
<s:Button right="10" bottom="39" label="Controlla" click="get_count();"/>
In the label I got the correct value, but I can't save and show the value into/from a simple variable...
That's because using the keyword as will make Flex to try to cast a type to another, with a risk that the cast fails. Here, you try to cast a String as an integer, which basically means you do the following :
take 'blabla' and check if it's an integer. If it is, put its value in
count, otherwise put null in it.
What you want to do is transtype (not sure about the word, though) a String to an integer. To do this, use the following syntax :
count = int(Number(counttotalResult.lastResult));
The above means
Take lastResult, and convert it into a Number, and then into an int.
The conversion can fail (but it wont, as long as lastResult is the string representation of a valid number), and the syntax could be shorter, but in a nutshell, that's the difference between casting a type to another, and converting a type to another.
How to convert UTC time into date time format in flex. I am using sdk 3.5. for example I have current date time in UTC format as 1309522586000 (milliseconds) and I want to convert it to friday jul 1 2011. How can I do this??
Thanks
If your are using a UNIX timestamp that you are retrieving from your server, first you will have to multiply it by 1000.
This is because UNIX timestamps are expressed in seconds whereas ActionScript timestamps are expressed in milliseconds.
You can create a date from your timestamp as follows:
var myDate:Date = new Date(1309522586000);
Next, you create a formatDate function that you call with myDate as parameter:
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:DateFormatter id="myDF" formatString="EEEE MMM D YYYY"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
private function formatDate(date:Date):void{
trace(myDF.format(date));
}
]]>
</fx:Script>
Notice that I am using a dateformatter to format the date correctly.
More about DateFormatter and possible formats here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/formatters/DateFormatter.html
Cheers
SCinitiationtarget.selectedDate = new
Date(rows1[i]['InitiationTarget']);
I am setting my seletedDate in my DateChooser like this. The format i am getting from the Database is 2009-12-30.
Its displaying in correctly.
I believe the date object doesn't recognize the dash as a valid separator. You'll have to some how reformat your date objects.
For example this works:
var date:Date = new Date("2009/12/30");
myDateChooser.selectedDate = date;
But this doesn't:
var date:Date = new Date("2009-12-30");
myDateChooser.selectedDate = date;
For more information on what date formats are valid, see the documentation here: http://livedocs.adobe.com/flex/3/langref/Date.html#Date%28%29
The first argument of Date constructor is called yearOrTimeValue and as its documentation says it accepts either year or time in UTC milliseconds. For proper Date construction use:
new Date(2009, 12, 30)
I got the solution finally.
var dateStr:String = dateFormatter.format(rows1[i]['InitiationTarget']);
SCinitiationtarget.selectedDate = new Date(dateStr);
<mx:DateFormatter id="dateFormatter" formatString="MMM D, YYYY"/>
With this the problem gets solved.
Why not use the parse method of the Date class?
SCinitiationtarget.selectedDate = Date.parse(rows1[i]['InitiationTarget']);