locked
Compare two dates in the script functoid RRS feed

  • Question

  • Hello everyone , 

    I have a situation here where i need to compare the dates. Let me tell the situation how i need to do it , 

     Input date will be in this format " 2016-03-28 15:21:34.107 " 

    I need to compare the Input date and see if its greater than the yesterday date in the same format. 

    If the condition is satisfied then the result should be "True" which is output. 

    If not then the result should be "False".

    Please do help me with this as i facing difficult to get the solution.

    Sukra.




    • Edited by sukra1234 Friday, May 6, 2016 1:26 PM
    Friday, May 6, 2016 1:26 PM

Answers

  • Use an inline C# script as follows. As input connect the input date field-

    public string CompareDates(string inputDate)
            {
                DateTime t;
                if (!DateTime.TryParse(inputDate, out t))
                {
                    return "False";
                }
    
                else
                {
                    DateTime y = DateTime.Now.AddDays(-1);
                    int result = DateTime.Compare(t, y);
                    if (result < 0)
                        return "False";
                    else
                        return "True";
                }
            }




    Friday, May 6, 2016 1:51 PM
    Moderator
  • A better version that handles invalid dates-

    // For an invalid date, this always returns False

    public string CompareDates(string inputDate)
            {
                DateTime t;
                if (!DateTime.TryParse(inputDate, out t))
                {
                    return "False";
                }

                else
                {
                    DateTime y = DateTime.Now.AddDays(-1);
                    int result = DateTime.Compare(t, y);
                    if (result < 0)
                        return "False";
                    else
                        return "True";
                }
            }


    Thanks Arindam



    Friday, May 6, 2016 2:17 PM
    Moderator

All replies

  • Use an inline C# script as follows. As input connect the input date field-

    public string CompareDates(string inputDate)
            {
                DateTime t;
                if (!DateTime.TryParse(inputDate, out t))
                {
                    return "False";
                }
    
                else
                {
                    DateTime y = DateTime.Now.AddDays(-1);
                    int result = DateTime.Compare(t, y);
                    if (result < 0)
                        return "False";
                    else
                        return "True";
                }
            }




    Friday, May 6, 2016 1:51 PM
    Moderator
  • Thanks Arindam , 

    Following is the error i am getting for the above code ,

     Invalid token 'if' in class, struct, or interface member declaration

    Inline Script Error: Type expected

    error btm1021: Inline Script Error: Invalid token '0' in class, struct, or interface member declaration


    ashwanth

    Friday, May 6, 2016 1:58 PM
  • Thanks Its working now :) 

    ashwanth

    Friday, May 6, 2016 1:59 PM
  • A better version that handles invalid dates-

    // For an invalid date, this always returns False

    public string CompareDates(string inputDate)
            {
                DateTime t;
                if (!DateTime.TryParse(inputDate, out t))
                {
                    return "False";
                }

                else
                {
                    DateTime y = DateTime.Now.AddDays(-1);
                    int result = DateTime.Compare(t, y);
                    if (result < 0)
                        return "False";
                    else
                        return "True";
                }
            }


    Thanks Arindam



    Friday, May 6, 2016 2:17 PM
    Moderator
  • //remove time format from date element
    Console.WriteLine(dtParam1.ToString("dd/MM/yyyy")); 
    
    //yesterday
    DateTime yesterday = DateTime.Now.Date.AddDays(-1);
    Console.WriteLine(yesterday.ToString("dd/MM/yyyy")); 
    
    int ans = DateTime.Compare(dtParam1, yesterday);
    
    //your true-false return logic here
    if(ans < 0)
    // ret
    else
    

    Something like this. You can put it in one scripting functoid or multiple, it's your choice.

    P.S: I guess you already have the answer. By the time I click 'submit'. good luck ;)

    Friday, May 6, 2016 2:26 PM
  • So, what problems are you having exactly?

    You should always use TryParse with string dates.  Such as this code:

            private bool DateGreaterThenYesterday(string testDate)
            {
                DateTime dtDate;
    
                if (DateTime.TryParse(testDate, out dtDate))
                {
                    dtDate = dtDate.Date;
                    if (dtDate >= DateTime.Today) return true;
                }
                return false;
            }

    Note: Greater then or equal to Today is the same as after Yesterday.

    Friday, May 6, 2016 2:43 PM
    Moderator
  • For clarity, the original marked Answer is not correct.

    It's best to wait to see if there are multiple proposals.

    Friday, May 6, 2016 2:49 PM
    Moderator
  • Sorry, but this answer is still incorrect. The valid flag is never used.

    A better version that handles invalid dates-

    // For an invalid date, this always returns False

    public string CompareDates(string inputDate)
            {
                DateTime t;
    	    bool valid = DateTime.TryParse(inputDate, out t);
                DateTime y = DateTime.Now.AddDays(-1);
                int result = DateTime.Compare(t, y);
                if (result < 0)
                    return "False";
                else
                    return "True";
            }


    Thanks Arindam



    Friday, May 6, 2016 2:51 PM
    Moderator
  • For an invalid date though, t would be set to DateTime.MinValue. So, from an execution/logical point of view, the above code is fine.

    But, probably not from a readability point of view.


    Thanks Arindam

    Friday, May 6, 2016 4:39 PM
    Moderator
  • Well, yes, but mostly no because it address the case in an indirect way.

    Since you agree that it's just bad form, sorry, it is.  You should either correct the code in both posts (for the benefit of future readers) or remove them.
    Friday, May 6, 2016 5:14 PM
    Moderator
  • Noted and edited.

    Thanks Arindam

    Friday, May 6, 2016 5:36 PM
    Moderator