locked
Comparing Dates RRS feed

  • Question

  • Hi,

    I have created a custom entity which has a self-refrential relationship so that we can acheive a parent child relationship. However, this entity has date fields (startDate and endDate).  These fields accept a date value. With the self - refrential relationship on this entity I have a lookup attribute on the form so that I can assign a parent record to a child record. I need to put a validation here so that a record with start date less than the start date of the child record can only become a parent. Otherwise, the validation should prompt the user to select another territory. I am able to fetch the dates for both the records but they are in completely different format. I am getting the start date for the child record as

    crmForm.all.flk_startdate.DataValue

    For the parent record I am calling the webservice through the javascript and fetching the value. Please help.

    Tuesday, March 23, 2010 10:37 AM

Answers

  • First Convert the date retrieved through webservice in the desired format and then obtain day, months, year. Both the dates are in UTC?

    function StringToDateTimeConvert(source)
    {
    var source1 = source.split('T')[0];
    var parts = source1.split('-');
    var dat = new Date();
    dat.setYear(parts[0]);
    dat.setMonth(parts[1] - 1);
    dat.setDate(parts[2]);
    return dat;
    }

    var _date = StringToDateTimeConvert('2009-12-30T00:00:00+02:00');

    alert(_date );
    alert(_date.getDate());
    alert(_date.getMonth());
    alert(_date.getFullYear());

    Regards
    Vinoth

    • Proposed as answer by VinothBalasubramanian Wednesday, March 24, 2010 5:23 AM
    • Marked as answer by mynameissid Wednesday, March 24, 2010 10:06 PM
    • Marked as answer by mynameissid Wednesday, March 24, 2010 10:06 PM
    • Marked as answer by mynameissid Wednesday, March 24, 2010 10:06 PM
    • Marked as answer by mynameissid Wednesday, March 24, 2010 10:06 PM
    Tuesday, March 23, 2010 2:53 PM

All replies

  • You can compare like the following:

    if(crmForm.all.flk_startDate.Value.getTime() > <otherDate>.getTime())

    {

    }

     

    getTime gives you the number of milliseconds since midnight of January 1, 1970


    My blog : http://mscrmtools.blogspot.com
    You will find:
    Bulk Delete LauncherView Layout replicator
    ISV.Config Manager Form Javascript Manager
    Assembly RecoveryAnd others (use tool tag on my blog)
    Tuesday, March 23, 2010 10:44 AM
    Moderator
  • I need to compare the two dates. Will this do??
    Tuesday, March 23, 2010 11:19 AM
  • following is the example code for this..

    chnage the value for fromDate and toDate

    // make sure start date is before the end date
    if (Date.parse(fromDate) > Date.parse(toDate)) {
    alert("Invalid Date Range!\nStart Date cannot be after End Date!")
    return false;
    }

    hope this helps..

     

    Tuesday, March 23, 2010 11:31 AM
  • Hi Mayank,

    This code is OK. The problem is that I am retreiving the date for the parent entity record by calling webservice from the javascript. This javascript fires onChange of the parent record attribute. However, when I am performing a Date.parse on the start date obtained by calling webservice from the javascript for the parent object record it is reutrning me a NAN. Heneforth, I cann't compare the dates in this way.

    Tuesday, March 23, 2010 11:40 AM
  • Hi

    crmForm.all.flk_startdate.DataValue will provide a value in the format 'Wed Dec 30 17:07:34 UTC+0530 2009'

    You cam convert you date (retrieved through webservice) to the above desired format and then perfrom then compare those

    sample:

    function StringToDateTimeConvert(source)
    {
    var source1 = source.split('T')[0];
    var parts = source1.split('-');
    var dat = new Date();
    dat.setYear(parts[0]);
    dat.setMonth(parts[1] - 1);
    dat.setDate(parts[2]);
    return dat;
    }

    var _date = StringToDateTimeConvert('2009-12-30T00:00:00+02:00');

    if(crmForm.all.flk_startdate.DataValue < _date)
    {
    //Perform action
    }
    else
    {
    alert();
    }

    Regards
    Vinoth

    • Proposed as answer by Ahmed ElNoby Tuesday, March 23, 2010 11:47 AM
    • Unproposed as answer by mynameissid Tuesday, March 23, 2010 12:09 PM
    Tuesday, March 23, 2010 11:44 AM
  • Hi Vinoth,

    This code is a saver. However, in this case, the records with same dates and different time stamp will throw up a prompt. The dates may be same say 23rd March but the timestamp will be different. What if I want to compare only the date part?

    Tuesday, March 23, 2010 12:10 PM
  • use substr() to cut down time stamp and compare date.

    crmForm.all.actualstart.DataValue.substr(0,9);

    if(crmForm.all.flk_startdate.DataValue.substr(0,9) < _date.substr(0,9))

    Regards
    Vinoth

     

    Tuesday, March 23, 2010 12:16 PM
  • This is what I am getting after implementing the substr function as specified above -

    Object doesn't support this porperty or method.

    Tuesday, March 23, 2010 12:30 PM
  • Yes because substr is a string function...

    you could try

    crmForm.all.actualstart.DataValue.toString().substr(0,9);

    But I'm not sure it will compare dates, more string...


    My blog : http://mscrmtools.blogspot.com
    You will find:
    Bulk Delete LauncherView Layout replicator
    ISV.Config Manager Form Javascript Manager
    Assembly RecoveryAnd others (use tool tag on my blog)
    Tuesday, March 23, 2010 12:38 PM
    Moderator
  • In this case, the date will convert to a string. It will not be possible to compare string values based on '<' and '>'
    Tuesday, March 23, 2010 12:58 PM
  • Maybe you can parse back to a date...
    My blog : http://mscrmtools.blogspot.com
    You will find:
    Bulk Delete LauncherView Layout replicator
    ISV.Config Manager Form Javascript Manager
    Assembly RecoveryAnd others (use tool tag on my blog)
    Tuesday, March 23, 2010 1:00 PM
    Moderator
  • You can use as suggested by Tanguy, as he said will be a problem during comparing.

    So you can Get day, month, year induvidually as below and  then compare to fulfill your requirement.

    crmForm.all.actualstart.DataValue.getDate();
    crmForm.all.actualstart.DataValue.getMonth(); 
    crmForm.all.actualstart.DataValue.getFullYear(); 

    Regards
    Vinoth

    Tuesday, March 23, 2010 1:02 PM
  • I can covert the date for the child start date attribute using the way suggested, but the main issue is posed by the start date of the parent which is in UTC format. It does not works the same way when it comes to extracting the required values from it to form a date. Any suggestions?
    Tuesday, March 23, 2010 2:44 PM
  • First Convert the date retrieved through webservice in the desired format and then obtain day, months, year. Both the dates are in UTC?

    function StringToDateTimeConvert(source)
    {
    var source1 = source.split('T')[0];
    var parts = source1.split('-');
    var dat = new Date();
    dat.setYear(parts[0]);
    dat.setMonth(parts[1] - 1);
    dat.setDate(parts[2]);
    return dat;
    }

    var _date = StringToDateTimeConvert('2009-12-30T00:00:00+02:00');

    alert(_date );
    alert(_date.getDate());
    alert(_date.getMonth());
    alert(_date.getFullYear());

    Regards
    Vinoth

    • Proposed as answer by VinothBalasubramanian Wednesday, March 24, 2010 5:23 AM
    • Marked as answer by mynameissid Wednesday, March 24, 2010 10:06 PM
    • Marked as answer by mynameissid Wednesday, March 24, 2010 10:06 PM
    • Marked as answer by mynameissid Wednesday, March 24, 2010 10:06 PM
    • Marked as answer by mynameissid Wednesday, March 24, 2010 10:06 PM
    Tuesday, March 23, 2010 2:53 PM
  • yes, the startdate for the child record is in UTC format. The function StringToDateTimeConvert specified by you converts the date retrieved from the call to webservice using javascript into the UTC format. Henceforth, both of them are in UTC now.
    Tuesday, March 23, 2010 3:00 PM
  • Since both of them are in UTC, you can compare them by splitting them.

    do you face ant problem in it.?

    Tuesday, March 23, 2010 3:14 PM
  • Thats what I am trying as of now.
    Tuesday, March 23, 2010 3:29 PM