How to ask if a date value is empty

Comments

8 comments

  • Rainer Grabowski (FVM)

    Hi,

    this code should do what you want:

    var date1 = @date1fFeld;
    var date2 = @date2Field;
    var today = moment();
    var diff1 = date1.diff(today, 'days');
    var diff2 = date2.diif(today, 'days');
    
    date2 != null ?  diff2 : diff1
    

    (read as: IF date2 not empty than calculate diff2, else diff1)

    But be careful: "today" is a static value, it's only been updated if one of the variables in the calculation field changes.
    Example: You enter the date 2 value today (Tuesday, 24.02. )Then you have the values day2 = 28.02.2015 and today = 24.02.2015 = result: diff2 = 4 days: But this result "4 days" will also been shown tomorrow (25.02.).

    Rainer

    0
    Comment actions Permalink
  • Hamid

    Hi,
    you can use this variant if you want :

    var d1 = moment(@date_field_1);
    var d2 = moment(@date_field_2);
    var D1 = d1.fromNow(true);
    var D2 = d2.fromNow(true);
    
    d2.isValid() ? D2 : D1
    

    /Hamid

    0
    Comment actions Permalink
  • Rainer Grabowski (FVM)

    Hi Hamid & Julian,

    yes, you can do it that way. But there is an important difference:
    .fromNow() delivers the result as a text string, .diff delivers a number. If you need the result for further calcutions, it's better to have a number.

    Another difference: With .diff you define more confortable in which unit the result should be shown, If you use .fromNow it sets the unit automatically (or you have to write some more code).

    I'm sorry, but in my above code there are typos und mistakes (did it to fast). Here's the correct code:

    var date1 = moment(@date1Field) ;
    var date2 = moment(@date1Field);
    var today = moment();
    var diff1 = date1.diff(today, 'd','h');  //using 'd','h' delivers a result like 1,25 days, only 'd' results in 1
    var diff2 = date2.diff(today, 'd','h');
    
    date2 != null ?  diff2 : diff1 
    

    (instead of != null one can use .isValid() as well)
    Rainer

    0
    Comment actions Permalink
  • JJ Julian Jordan

    Reiner thanks a lot for your help, I really appreciate it. Following what I learned from your post, I decided to create a calculated field where I would have the latest date. The app is for event management and it has two dates: session1 and session2. Each event has either one session or two, so, in some cases date2 will be empty. I tryed to calculate the latest date in each event:

    var date1 = moment(@session1).format("DD MMM");
    var date2 = moment(@session2).format("DD MMM");

    date2!= null ? date2 : date1
    date2

    With this code, I get the correct result if there is a session2 but I get "Invalid date" if there is no date2.

    What am I doing wrong?

    Thanks a lot

    0
    Comment actions Permalink
  • Hamid

    you must delete the last line in your code 'Date2'

    0
    Comment actions Permalink
  • JJ Julian Jordan

    Thanks Hamid, I did delete the last line, but keep getting "Invalid Date" when the Date2 field is empty :(

    0
    Comment actions Permalink
  • Hamid

    Have you try with isValid() like this :

    date2.isValid() ? date2 : date1

    0
    Comment actions Permalink
  • Rainer Grabowski (FVM)

    Julian, there's a typo in your code:
    date2!= null
    must be
    date2 != null
    space after 2

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk