convert UTC time to date time format in flex? - apache-flex

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

Related

Convert datepicker date to ISO format with moment.js

My task is to use a datepicker to pick a date in the prescribed format, eg(MM-DD-YYYY) and pass it to the server as ISO formatted.
While it test the output the ISO formatted date is one day behind.
For example
If i select
07-13-2015
My Output ISO format is
ISO format is :2015-07-12T18:30:00.000Z
Here you can see date is 13 but the output date is 12
I am from India. I tried with zone and utcOffset, ended up with no results. How do i set it right
Here is the JSFIDDLE
js code
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(moment(selectedDate).toISOString());
});
I do have a hidden field which value will be updated on change and that will be processed in the server. No issues on that.
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(selectedDate.toISOString());
});
Your selectedDate is already a moment object so you do not need to feed it back into another moment.
Example:
var test = '07-13-2015'
var mtest = moment(test,"MM-DD-YYYY")
mtest.toISOString()
"2015-07-13T06:00:00.000Z"
Your could try converting the date format to UTC at once.
selectedDate = moment(selectedDate).utc('MM-DD-YYYY')
According to http://dygraphs.com/date-formats.html, if you pass a string like '07-13-2015', it means Midnight of 13th July 2015. Now, if you use toISOString function, it will convert it to UTC by default. To not convert it to UTC, just pass a parameter true in the toISOString function. (Moment.js docs)
For example:
var date = '07-13-2015';
date = moment(date,'MM-DD-YYY');
console.log(date.toISOString(true));
This way, moment will not convert the date to UTC.

how get 24 hour date from DateField in flex?

Normally we can get this("2014-04-24 04:50:10 PM") type of date and time from dateField.I need 24 hour date format like this "2014-04-24 16:50:10" . Is this possible ?
You need to use dateformatter
<mx:DateFormatter id="dateFormatter" formatString="YYYY-MM-DD HH:NN:SS"/>
An example is at the very end here

ColdFusion: Error Passing Date that doesn't have day of week?

I am trying to parse two date strings into date objects. The code works for one string, but throws an "invalid date time" error for the other. The only difference is that it's got a "Sat, " at the beginning! Please tell me why this is happening and how I can solve it!
<cfset datetimetest1 = "23 Nov 2013 00:53:12 +0000">
<!--- ^ This throws an error (when you try to pass it). Error says 'invalid date time' --->
<cfset datetimetest2 = "Sat, 23 Nov 2013 00:53:12 +0000">
<!--- ^ This works when it is parsed --->
<cfoutput>
#parsedatetime(datetimetest1)# #parsedatetime(datetimetest2)#
</cfoutput>
I identified RSS dates that are being used from the following sources and tested them against isDate(), DateFormat() and LSDateFormat(). ColdFusion 10,286680 was only able to parse 65% of the dates (38 out of 58).
http://rssdateformats.tumblr.com/
https://github.com/mjibson/goread/blob/0387db10bd9fd9ccd90d557fa30b6e494efa577a/goapp/utils.go#L129
Here's the test script that I wrote:
https://gist.github.com/JamoCA/7617349
I've been looking for a Java date parser library and recently found Natty and StringToTime, but haven't used either yet. (Neither resource provides a downloadable JAR file.):
http://natty.joestelmach.com/
https://github.com/collegeman/stringtotime
Update:
As of CF10+, you can use a custom format string to instruct the function on how to parse and convert the input into a datetime object:
// Custom format string
dateObject = parseDateTime("23 Nov 2013 00:53:12 +0000"
, "dd MMM yyyy HH:mm:ss zzz");
This is one area in which CF's flexibility and ease of use can be a disadvantage IMO. Unfortunately parseDateTime does not allow you specify the format of the input string, so CF has to do a lot of guessing to "automagically" convert your string into a date object.
One of the tools CF employs is java's SimpleDateFormat class, which utilizes patterns to parse or convert strings into Dates. My understanding is CF maintains a listing of standard date/time patterns (according to U.S. date conventions). Your first string must not match any of those patterns. Hence the error.
If your date strings are always in UTC, you could simply use list functions to omit the timezone offset ie +0000, then parse the string as usual:
<cfscript>
origString = "23 Nov 2013 00:53:12 +0000";
dateString = listFirst(origString, "+");
WriteDump(parseDateTime(dateString));
</cfscript>
Or you could DIY using SimpleDateFormat and the appropriate pattern dd MMM yyyy HH:mm:ss Z (see Date and Time Patterns). Just note the returned dates are in local time, so you may need to use DateConvert if you want UTC times:
// get formatter based on default locale
formatter = createObject("java", "java.text.SimpleDateFormat").init();
// set up pattern for input date string
formatter.applyPattern("dd MMM yyyy HH:mm:ss Z");
// parse it into a date object
dateObject = formatter.parse("23 Nov 2013 00:53:12 +0000");
// display result in local and UTC time
WriteDump("local="& dateObject);
WriteDump("utc="& DateConvert("local2UTC", dateObject));

How to save the Date as DD/MM/YYYY format in SQLITE Flex AIR application

i am doing a flex Adobe AIR Application with SQLITE Data base,in that i want to save Date in the following format DD/MM/YYYY, but in my SQLITE TABLE i gave the data type is DATE so it saved as Sun Dec 2 00:00:00 GMT+0530 2012 in this format.
why i want to save in specific format.i have two date fields START DATE and END DATE.Once i select the End DATE date field i have to count the days between them automatically.any ideas for DD/MM/YYYY format and counting the date in between the dates.
This is my code:
[Bindable] private var date1:Date;
[Bindable] private var date2:Date;
[Bindable] private var Totaldays:String;
protected function modEndDate_creationCompleteHandler(event:FlexEvent):void
{
formatter.formatString= "DD-MM-YYYY";
date1=formatter.format(newStartdate.selectedDate);
date2=formatter.format(newEndDate.selectedDate);
**Totaldays=Math.floor(Math.abs(date1 - date2)/ (24 * 60 * 60 * 1000))**
}
The error is:
Multiple markers at this line:
-1067: Implicit coercion of a value of type Date to an unrelated type Number.
-1067: Implicit coercion of a value of type Number to an unrelated type String.
Looking for help.Thanks in Advance
Regards,
Venkatesan
Save in compatible format. Format dates in GUI as you like. Look here to calculate days between: ActionScript 3.0 + Calculate timespan between two dates?

flex3 Format date without timezone

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.

Resources