Hitachi Vantara Pentaho Community Forums
Results 1 to 5 of 5

Thread: JavaScript compare dates not working

  1. #1
    Join Date
    Nov 2016
    Posts
    20

    Question JavaScript compare dates not working

    Hi there,

    I can't get dates to compare properly in a "Modified Java Script Step".

    Note: assume that for now this HAS to be done in JavaScript... this is just a cut-down transformation to highlight the issue I'm having.

    In the attached example, I would expect that the "is_most_recent_vehicle" bool value would be Y on rows 1 and 8, then N for all other rows.

    But unfortunately they are all coming through as Y.

    I suspect this has something to do with JS comparing the object types and not values. That said, I have tried converting to text with date2str and putting truncDate around the dates and then comparing with no luck. I tried comparing them as ints in this example after reading a forum post here but it's still not working. Perhaps I am using my "init" tab (a "Start Script") incorrectly?

    If anyone can offer some help on this one, that would be great! Thanks!


    Regards,

    Stanbridge
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2015
    Posts
    107

    Default

    You are not ever setting or *resetting* is_most_recent_vehicle to false, so after the first true it remains true.

    I think it should be

    if (parseInt(int_vehicle_date) == parseInt(int_last_date)) {
    is_most_recent_vehicle = true;
    }
    else { is_most_recent_vehicle = false;
    }

  3. #3
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    Even though you seem to be determined about using Rhino (embeddable JavaScript interpreter written in Java with fields exposed as Java objects) for this, do yourself a favor and minimize your lines of code by utilizing some convenient steps:

    • Sort-Rows (vehicle_id, vehicle_date) ensures the much needed order of rows.
    • Analytic-Query let's you carry over fields from a previous row.
    • Calculator is able to compute the number of days between two dates.
    So long, and thanks for all the fish.

  4. #4
    Join Date
    Nov 2016
    Posts
    20

    Default

    Hi Marabu,

    Yep... it was that damned ELSE that I wasn't including! I was forgetting"false" in my init only happens once That's got it, thanks.

    Yes in my "real" ETL there's some pre-sorted date coming in. Also, I have a few comparisons to do (not just dates) such that it's easier to see it all in one window. But points well taken. I'll definitely checkout that analytic query though!

    Thanks mate!

  5. #5
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    I didn't even comment on your JavaScript skills.
    So long, and thanks for all the fish.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.