Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGL Development Tools » How do you convert a string to a date?
How do you convert a string to a date? [message #1015118] Wed, 27 February 2013 14:08 Go to next message
Richard Moulton is currently offline Richard MoultonFriend
Messages: 92
Registered: August 2011
Location: Devon, UK
Member
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.
d date = "25/12/2012";


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 14:49 Go to previous messageGo to next message
Matt Heitz is currently offline Matt HeitzFriend
Messages: 36
Registered: July 2009
Member
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 #1015245 is a reply to message #1015137] Wed, 27 February 2013 21:13 Go to previous messageGo to next message
Richard Moulton is currently offline Richard MoultonFriend
Messages: 92
Registered: August 2011
Location: Devon, UK
Member
Matt,

Yes, I'd seen the comments in the source but at the back of my mind I was sure I'd seen a function to perform date conversion, possibly in one of the supplied libraries, though this may have been in EGLCE or RBD.

Thanks for the confirmation, I'll write a function to perform the conversion.

Richard
Re: How do you convert a string to a date? [message #1015264 is a reply to message #1015245] Wed, 27 February 2013 22:52 Go to previous message
Richard Moulton is currently offline Richard MoultonFriend
Messages: 92
Registered: August 2011
Location: Devon, UK
Member
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
Previous Topic:EGL Program: parameters to main function
Next Topic:What's next???
Goto Forum:
  


Current Time: Fri Dec 19 01:47:07 GMT 2014

Powered by FUDForum. Page generated in 0.02942 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software