How do you convert a string to a date? [message #1015118] |
Wed, 27 February 2013 09:08  |
Eclipse User |
|
|
|
Is there a built in function to convert a string to a date?
I have an input field for a date value, which is entered in DD/MM/YYYY format. I would like to convert this to a date variable.
I tried the following but this appears to assume my date format is MM/DD/YYYY and I can't work out if there's a method of defining the date format that should be used.
I could create a function to do this but wondered if there was a function already built in to EDT. I couldn't find anything in the documentation.
Richard
|
|
|
Re: How do you convert a string to a date? [message #1015137 is a reply to message #1015118] |
Wed, 27 February 2013 09:49   |
Eclipse User |
|
|
|
Hi Richard,
Here's the comment on the operator that converts strings to dates. It explains what happens when you assign a string to a date.
/**
* {@Operation narrow} Converts a string to a date. The string is parsed
* by searching for the month, then the day, then the year. One or two digits
* can be specified for the month and day. The year requires a minimum of one
* digit and a maximum of at least four digits (in other words, some implementations
* can support years beyond 9999). One separator character is required between
* the month and day, and another between the day and year. The separator
* character can be anything, even a digit (though that's probably a bad idea)
* and the two separator characters don't have to be identical.
*
* @throws TypeCastException if the string can't be parsed into a date.
*/
This operator is the function EDate.asDate(). I believe you can see the source if you type EDate in the editor, select it, and press F3. The same goes for the rest of our data types. They're all defined in .egl files and you can see their source so you know what all the functions do. The comments are written in a style similar to Javadoc so that we might make a tool to produce API docs from the source code.
We don't have a built-in function that does the conversion using a date format that you specify. We do have the reverse: StringLib.format(date, formatStr). Maybe we should add StringLib.parse(dateStr, formatStr).
-Matt
|
|
|
|
Re: How do you convert a string to a date? [message #1015264 is a reply to message #1015245] |
Wed, 27 February 2013 17:52  |
Eclipse User |
|
|
|
Just in case anyone might find it useful, I've attached a project with a DateHelperLib library part (built with EDT 0.8.2). This includes a format() function that takes a date string and optional date format string and returns a date variable.
The only format string that it deals with (at the moment) is "dd/MM/yyyy", which is also the default, if the format string is not passed. I love overloaded functions.
There's a sample RUI handler that demonstrates the function.
Richard
|
|
|
Powered by
FUDForum. Page generated in 0.03464 seconds