YYYY/MM format in Knime

Good Day Knimers :green_heart:

Is it possible to get only year and month as date format?
Example:2021/09 - YYYY/MM (date data type)

Thanks a lot :relaxed:

Hello @Karlygash,

simply use yyyy/MM in Date&Time to String node. This is under assumption that your date is in column type Local Date. Note that any other Date(&Time) format different than default (yyyy-MM-dd…) must be in string column. So when working with date (and time) information it’s best to keep it in Local Date (Time) column type so you can utilize Date&Time nodes (e.g. Date&Time Difference node) and prior to export/visualization convert it to string format you want/need.

Br,
Ivan

2 Likes

Hi @ipazin , I think @Karlygash is trying to achieve the other way around.

The way I understood it is that the data is like β€œ2021/09” as string, and @Karlygash wants to convert the data type to date, so converting string β€œ2021/09” to a date format.

@Karlygash , I don’t think that is possible as this is not a valid date. What is the purpose of doing this? Could you append β€œ/01” to all of these to make it a valid date string? So β€œ2021/09” would have to be transformed to β€œ2021/09/01” first.

But as I mentioned, perhaps understanding the purpose behind this can help determine what the best solution would be.

4 Likes

@bruno29a @ipazin
Good Evening) :innocent:
Yes, actually I added β€œ/01” and converted them into date format.
In that case I could compare two date formats
Thanks a lot :hugs:

3 Likes

Hi @Karlygash , if you need to compare dates to retrieve difference between them in different granular (days, months, year, etc), then yes you need to convert, and you figured out with the β€œ/01”.

If you just need to compare to see which date is smaller or bigger (earlier or later), you can compare as string, as long as your format is in YYYY followed by MM (any separator in between does not matter, whether β€œ-” or β€œ/” or any other). For example, as string, β€œ2021/08” is smaller than β€œ2021/09” in the sense that alphabetically ordered, β€œ2021/08” comes before β€œ2021/09”.

So, you can actually evaluate that β€œ2021/08” < β€œ2021/09”.

Here’s a test workflow that tests this via Column Expressions and Rule Engine:
image

Input data:
image

Column Expressions is set as this:

if(column("column1") > column("column2")) {
    "Greater"
} else if(column("column1") < column("column2")) {
    "Less"
}

Results:
image

On the other side, Rule Engine is set up like ths:

$column1$ > $column2$ => "Greater"
$column1$ < $column2$ => "Less"
TRUE => "Same"

Results:
image

So, as you can see, you can compare strings in that sense.

3 Likes

Hello @bruno29a and @Karlygash,

glad you figured it out :slight_smile:

Br,
Ivan

2 Likes

Very thanks @bruno29a , @ipazin :innocent:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.