Hitachi Vantara Pentaho Community Forums
Results 1 to 6 of 6

Thread: JavaScript: Compare two dates

  1. #1
    Join Date
    Mar 2009
    Posts
    137

    Default JavaScript: Compare two dates

    Hi everybody,

    I have a problem comparing to dates from my db (StartTime and EndTime) using the JavaScrip Step in Pentaho:

    var theDate = STARTTIME;
    var theEnd = ENDTIME;

    Alert(theDate <= theEnd);
    Alert(theDate < theEnd);
    Alert(theDate == theEnd);
    Alert(theDate >= theEnd);

    Always returns false, Starttime and Endtime are not null, both contain real dates (e.g. 2008/12/03 00:00:00).

    Can anyone help?

  2. #2
    pstoellberger Guest

    Default

    I don't know what you're doing in the javascript step but you could use the filter step to check the dates and then process it like you want

    if you need the javascript step: probably its handling the 2 dates as strings instead of dates. you could try to use "var theDate = str2date(STARTTIME);"

  3. #3
    Join Date
    Mar 2009
    Posts
    137

    Default

    Quote Originally Posted by pstoellberger View Post
    I don't know what you're doing in the
    if you need the javascript step: probably its handling the 2 dates as strings instead of dates. you could try to use "var theDate = str2date(STARTTIME);"
    I need the javascript to step to add rows because of date information, I pretty sure that there is no other way...

    But your "solution" does not work, or better leads to an error:
    Couldn't execute this script! Error on position (2:0) :
    Could not apply local format : Format.parseObject(String) failed (script#2)

    If I alert theDate, I get:
    2008-08-24 15:15:00.0

    Which is the timestamp from the data source (Oracle).

  4. #4
    pstoellberger Guest

    Default

    try adding the date format:

    i think that would be:
    str2date("2008-08-24 15:15:00.0","yyyy-MM-dd hh:mm:ss.S");

  5. #5
    Join Date
    Mar 2009
    Posts
    137

    Default

    still the same error message... but I found something really weird... because of your hint I did the following:

    Code:
    var theStartDate = truncDate(STARTTIME, 1);
    var theEndDate = truncDate(ENDTIME, 1);
    
    if (theStartDate != null && theEndDate != null) {
    
        var theStartDay = truncDate(theStartDate, 3);
        var theEndDay = truncDate(theEndDate, 3);
    
        // Going through the days
        var theDay = truncDate(theStartDay, 1);
        for (; theDay <= theEndDay; theDay = dateAdd(theDay, "d", 1)) {
            Alert(1);
        }
    }
    And I added the variables to the output... so I get the following:

    STARTTIME 2008/08/24 15:15:00.000
    ENDTIME 2008/08/24 17:45:00.000
    theStartDate 2008/08/24 15:15:00.000
    theEndDate Sun Aug 24 17:45:00 CEST 2008
    theStartDay 2008/08/24 00:00:00.000
    theEndDay 2008/08/24 00:00:00.000
    theDay 2008/08/24 00:00:00.000

    It's weird, that theEndDate is formatted like that, isn't it? But it's for sure that theDay <= theEndDay... but the Alert(1) is never executed...

  6. #6
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    It has been mentioned before but it's also quite easy to get EPOCH time from a date.

    Code:
    var timeA = dateA.getTime();
    var timeB = dateB.getTime();
    
    var cmp;
    if (timeA < timeB) {
     cmp="Smaller";
    } else {
     cmp="Larger";
    }

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.