Convert an object to a date - actionscript-3

I have imported an xml file that contains a date stamp. I extracted the date stamp by placing it into an Object:
dataObject = new Object();
dataObject.date = ... etc.
The date stamp was created by a sql database and its structure is as follows, but it is no longer a Date:
2011-02-03 16:30:10
Can I convert the text in the Object back into a Date object within Flex to be able to use Date methods?

You can use Date::parse for this:
var date:Date = new Date(Date.parse(dateString));
However, note that Flash doesn't recognize "-" as a separator so you need to convert the string first. Something like this should work:
dateString = dateString.replace(new RegExp(/-/g), '/');
var date:Date = Date.parse(dateString);

Related

Google Script taking date format YYYY-MM-DD HH:MM:SS as text, need to change timezone

I have a response which is returning the dates as YYYY-MM-DD HH:MM:SS format. However, due to timezones issues, I am trying to convert these in to GMT format.
I tried directly converting them but Google script always takes these as Text and thus returns Jan 1, 1970 as the output when using a Date object.
I tried adding the timezone offset to the string and then converting, but to no avail.
Is there any way I can convert this format into a proper date or directly convert it into GMT?
dateValue = '2019-02-20 18:30:00'
var tempDate = dateValue + ' +0530';
dateValue = new Date(tempDate)
I'd expect the outcome to be returned as a date, which I can then convert to GMT format
How about this?
function splitItUp() {
var dts="2019-02-18 19:52:14";
var tA=dts.split(' ');
var tD=tA[0].split('-');
var tT=tA[1].split(':');
var dt=new Date(Number(tD[0]),Number(tD[1])-1,Number(tD[2]),Number(tT[0]),Number(tT[1]),Number(tT[2]))
Logger.log(dt);
}

Inserting date in Mysql (codename one)

I want to insert a Date object in mysql database, which has a Date type in the database as well. I am having problems inserting the date .
I have tried this code, but it seems codename one have a problem with it:
dateString s;
s = date.getCurrentMonth() + "/" + date.getCurrentDay() + "/" + date.getCurrentYear();
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = (Date) formatter.parse(s);
Please can you tell me how to do it ?
You don't need to format it. Just use this SQL Date Object instead of Date object from java.util package.
import java.sql.Date
// Creating a date object.
Date date = new Date();
In a database, make sure the data type of attribute 'date' is selected as "Date" also, not VarChar. Simply pass this sql package Date object into the database through query. :) It will save the date in a format.

JSON.NET: Get Specific JSON Date Value

In my VB.NET project, using JSON.NET, I've got a JSON from a Web API that has a value representing a date in the yyyy-MM-ddTHH:mm:ss format, and I'd like to simply get that value.
Here's more or less what the JSON looks like:
{
"REQ_DATE": "2016-01-17T12:27:57",
"REQ_TYPE": "Vacation",
"HOURS": 500.0,
"LEAVE_TIME": "8:00 AM",
"LEAVE_DATE": "2016-01-23T00:00:00",
"DUE_TIME": "8:00 AM",
"DUE_DATE": "2016-01-24T00:00:00",
}
So I should just serialize it and do what I will with the value, right? However, when I put that key-value in a variable, the date format is automatically changed!
Dim temp As String = myJsonResult("REQ_DATE")
' temp = "1/17/2016 12:27:57 PM"
I need to have that Date as it was from the retrieved JSON. I see two ways to go about this problem: converting it to yyyy-MM-ddTHH:mm:ss manually, or using regex to directly grab the key-value pair - both of which I have had no success with.
My attempt to convert it into DateTime:
Dim tempDateTime As DateTime = DateTime.ParseExact(myJsonResult("REQ_DATE").ToString,"yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture)
' Error: String is not Recognized as valid DateTime
Dim myDesiredResult As String = tempDateTime.ToString("yyyy-MM-ddTHH:mm:ss")
My attempt to use Regex:
Dim regex As Regex = New Regex("""REQ_DATE"": ""([\d\w]*)""")
Dim match As Match = regex.Match(myJsonAsString)
If match.Success Then
Dim myDesiredResult As String = match.Groups(1).Value
End If
' match is empty...
Any and all help is greatly appreciated.
I assume that myJsonResult is a JObject into which you have loaded your JSON.
Your difficulty is that Json.NET automatically recognizes strings as dates when reading and converts them to DateTime structs. Then when you later do ToString() on the value token, it comes back in c#'s "invariant culture" format rather than the original format, which in this case was ISO 8601. If you do not want this behavior, you can parse your JSON using JsonSerializerSettings.DateParseHandling = DateParseHandling.None:
Dim jsonString = "{'REQ_DATE':'2016-01-17T12:27:57','REQ_TYPE':'Vacation','HOURS':500.0,'LEAVE_TIME':'8:00 AM','LEAVE_DATE':'2016-01-23T00:00:00','DUE_TIME':'8:00 AM','DUE_DATE':'2016-01-24T00:00:00'}"
Dim settings = New JsonSerializerSettings() With { _
.DateParseHandling = DateParseHandling.None _
}
Dim myJsonResult = JsonConvert.DeserializeObject(Of JObject)(jsonString, settings)
Dim dateValue = myJsonResult("REQ_DATE")
Dim dateString = CType(dateValue, String) 'Value is 2016-01-17T12:27:57
There's no overload to JObject.Parse() that takes a JsonSerializerSettings, so you would need to use DeserializeObject. This setting eventually gets propagated to JsonReader.DateParseHandling.
Alternatively, if you are OK with Json.NET recognizing dates but would always like them to be printed in ISO 8601 format, you can re-serialize the token to JSON rather than just getting the string value:
Dim dateValue = myJsonResult("REQ_DATE")
Dim dateString = JsonConvert.SerializeObject(dateValue).Trim(""""c) 'Value is 2016-01-17T12:27:57
Prototype fiddle. Related c# question.
When you get the parsed date-string from JSON.NET it will be in your culture's format as shown in your code comment:
Dim temp As String = myJsonResult("REQ_DATE")
' temp = "1/17/2016 12:27:57 PM"
That format is "M/d/yyyy HH:mm:ss tt" so use that format for parsing. But since it is in a format for your culture, you can let DateTime check/use all the known formats using TryParse:
Dim dt As DateTime
Dim temp As String = jobj("REQ_DATE").ToString
If DateTime.TryParse(temp, dt) Then
'dt is good to use!
Console.WriteLine(dt.ToString)
' Prints: 1/17/2016 12:27:57 PM
End If
Dim myDesiredResult As String = dt.ToString("yyyy-MM-ddTHH:mm:ss")
' result: "2016-01-17T12:27:57"
If you want to specify the format (it would be better to use TryParseExact):
dt = DateTime.ParseExact(temp, "M/d/yyyy HH:mm:ss tt",
CultureInfo.InvariantCulture)
Console.WriteLine(dt.ToString())
' Also prints: 1/17/2016 12:27:57 PM
Dates dont have a format, so it cant change, but it may display differently for your culture settings. You can display it any way you want, it is still the same date:
Console.WriteLine(dt.ToString("HH yyyy MM mm dd"))
Console.WriteLine(dt.ToString("yyyy-MM-ddTHH:mm:ss"))
Console.WriteLine(dt.ToString("MM/d/yyyy HH:mm:ss tt"))
Answering my own question. I've got it to work simply by doing DateTime.Parse on the JObject, then doing ToString to my desired format.
Dim temp As String = myRequest("REQ_DATE").ToString
Dim tempDateTime As DateTime = DateTime.Parse(temp.ToString)
Dim myDesiredResult As String = tempDateTime.ToString("yyyy-MM-ddTHH:mm:ss")
I used dbc's answer in my code as it is more elegant.

DateField: selectedDate shows hours as 24:00:00

With a DateField component, the selectedDate.getHours returns as 24:00:00. I want it to return as 00:00:00.
Is there an easy way to do this?
Thanks!
UPDATE:
First, I set a variable in my Model that equals the selectedDate of a DateField component:
model.generalInfo.endDate = endDate_df.selectedDate;
Then I set another variable based on that value and I trace it out:
param.todate = df.format( model.generalInfo.endDate.toString() );
And this is where I see the time equal to 24:00:00
you could try something like
selectedDate.time = selectedDate.time - 24 * 60 * 60 * 60 * 1000
as a Date.time represents miliseconds since 1970 or whatever.. you substract 24 hours..
if it not works for you, you can create a new function or getter that converts it, or you can create a new mxml module, with DateField as superclass, and you can override the getHours method. tons of options to do this..
It looks like you are using the Flex DateFormatter to format the Date object. Have a look at the docs for this class, it has a formatString property that you can use to control how to output the date (or in this case the time).
If you give the DateFormatter a format string that contains "H" will output the hour in 24 hour format using the number range 1-24. If the format string contains "J" it will output the hour in 24 hour format using the range 0-23.
To get your desired output, use "JJ" in the format string, in addition to any other items. For example to output the hours, minutes, seconds:
var someDate:Date = new Date(2012, 11, 5);
var df:DateFormatter = new DateFormatter();
df.formatString = "JJ:NN:SS";
var formatted:String = df.format(someDate); // 00:00:00
Also, as #Flextras mentioned, there is Flash localization API you can use which has the added benefit of converting date/time strings to the values used by their locale. These are the DateTimeFormatter classes:
fl.globalization.DateTimeFormatter
spark.formatters.DateTimeFormatter (Flex 4)
These classes format dates into the user's locale (or one that you specifcy), and format Date objects the same way the DateFormatter does.

How to get item value from an ArrayCollection?

I trying to read a value from an ArrayCollection, I use getItemAt and get an object:
masterData.getItemAt(0,0)
Then i use: masterData.getItemAt(0,0).toString(); and get:
<d>
<i>The value that I need</i>
</d>
Now How can I get the value in "< i >" tag?
One approach you can try is to parse the string you've been returned.
var theString:String = '<d> <i>The value that I need</i> </d>';
var startPosition = theString.search('<i>') + '<i>'.length;
var endPosition = theString.search('</i>');
trace (theString.substring(startPosition, endPosition));
In the code example above, which is overly verbose, I used a search for the postions of the substring because I didn't know the structure of the data you're expecting back.
You can convert string to XML ( if all of your items will pass format of XML )
and then retrieve what you need.
var xml:XML = XML ( masterData.getItemAt(0,0).toString() );
trace( xml.i )