locked
Breaking out Date Fields RRS feed

  • Question

  • How would I break a date/time field in CRM into two seperate fields that are autopopulated based on what is stored in the parent field.  I need to break a date/time field into two different fields showing the month and day.  Is this possible?

    Thanks

    Thursday, September 16, 2010 3:26 PM

Answers

  • you can create two field and use below code

    var Birth = crmForm.all.birthdate.DataValue;

    crmForm.all.<month field name>.DataValue=Birth.getMonth()+1;

    crmForm.all.<Day field name>.DataValue=Birth.getDate();

    Let us know if you need any other information


    Mahain : http://mahenderpal.wordpress.com
    Thursday, September 16, 2010 5:47 PM
    Moderator

All replies

  • you can create two integer field one for Month and one for Day, through JS code you can easily fetch Month and day to store in corresponding fields.

    you can refer http://www.w3schools.com/js/js_obj_date.asp


    Mahain : http://mahenderpal.wordpress.com
    • Proposed as answer by HIMBAPModerator Thursday, September 16, 2010 3:40 PM
    Thursday, September 16, 2010 3:40 PM
    Moderator
  • Put this code on change of parent field (after changing the fields names)

     

    var cd = new Date();
    cd = crmForm.all.<ParentField>.DataValue;
    var year = cd.getYear();
    var month = cd.getMonth()+1;
    if (month <=9)
    {
    month = '0'+month;
    }
    var day = cd.getDate();
    var hour = cd.getHours();
    var minute = cd.getMinutes();
    if (minute <=9)
    {
    minute = '0'+minute;
    }
    crmForm.all.<DateField>.DataValue = day+"/"+month;
    crmForm.all.<TimeField>.DataValue = hour+":"+minute;

    Thursday, September 16, 2010 3:43 PM
  • This code did not work.  Also I do not need time.  I am currently just looking to break out two fields.

    1. Month of Birth
    2. Day of Birth

    Both of these fields would be coming from the field Birthdate.

    Thursday, September 16, 2010 5:24 PM
  • you can create two field and use below code

    var Birth = crmForm.all.birthdate.DataValue;

    crmForm.all.<month field name>.DataValue=Birth.getMonth()+1;

    crmForm.all.<Day field name>.DataValue=Birth.getDate();

    Let us know if you need any other information


    Mahain : http://mahenderpal.wordpress.com
    Thursday, September 16, 2010 5:47 PM
    Moderator
  • I have tested and the code is working fine. I have modified the code according to your requirements as under:-

    var cd = new Date();
    cd = crmForm.all.birthdate.DataValue;
    var month = cd.getMonth()+1;
    if (month <=9)
    {
    month = '0'+month;
    }
    var day = cd.getDate();

    if (day <=9)
    {
    day = '0'+day;
    }
    alert(day);
    alert(month);
    crmForm.all.new_dayofbirth.DataValue = day;
    crmForm.all.new_monthofbirth.DataValue = month;

    Put the code on change of birthdate. Make sure event is enabled and publish it. Refresh contact form and change the Birthday on any contact you should get two alerts. When you are happy just comment the alerts.

    • Proposed as answer by Faisal Fiaz Thursday, September 16, 2010 5:58 PM
    Thursday, September 16, 2010 5:49 PM
  • Thanks this worked but I need this to run for the exisiting records also and not jsut when the Birthdate changes.  I added the code to the OnLoad script for the Form and now I am getting an error.  I think it has to do with records that currently do not have Birthdate populated.  How do I handle Birthdates that are null?
    Thursday, September 16, 2010 6:06 PM
  • Just use

    if(crmForm.all.birthdate.DataValue!=null)

    {

    var Birth = crmForm.all.birthdate.DataValue;

    crmForm.all.<month field name>.DataValue=Birth.getMonth()+1;

    crmForm.all.<Day field name>.DataValue=Birth.getDate();

    }


    Mahain : http://mahenderpal.wordpress.com
    Friday, September 17, 2010 5:44 AM
    Moderator