Question

String field to Date field


I'm trying to separate several years of data based on an install date which is input as a string field.

 

The data is manually input as a simple string (ie. "2019" no day or month information) and in an effort to keep from having to manually update the entire process at year's end I'm using a test filter with date/time functions that will pull out "Current Year", "Last Year", etc. for several years of data but I'm having trouble getting the string converted to a format that the date/time functions will recognize. I've tried using the string formatter to get the string into a number which I thought would register as a date automatically with FME but that doesn't seem to work either.

 

Any thoughts? This seems like such a simple issue I can't find a solution for.


2 replies

Userlevel 3
Badge +16

An FME date has form yyyymmdd (also described as %Y%m%d), so it needs a month part and a day part. If a month and day are not provided, then I don't think the DateTimeConverter can default it to something.

I'd use an AttributeCreator, with a Conditional Value to say, if the given 'date' matches regex ^[0-9]{4}$, then add a '0101' 1 January to the end of the string like: @Value(Current Year)0101

If there are more date formats used other than the four digit year, then you'll need to add a different rule for each case.

 

Once the the numbers are in that yyyymmdd form, then the date functions, writers etc will work with it as if it is a date.

Userlevel 1
Badge +21

Rather than convert the string to a date, you can convert the current year date to a value of just the year and use that in the test filter, e.g.

image

Reply