web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

adding a decimal number for a date

(0) ShareShare
ReportReport
Posted on by

Hi, 

I have a field "Start Date" date type = "Notice period" field Date type + "Duration in month" decimal type  

its working and setting the date correct if the Duration in month i'm adding is whole "for example 3 " but if " 3.5  " it  consider it as 3 months and setting wrong

any suggestion? 

function SetStartDate() 
{
   debugger;
   var Notice = Xrm.Page.getAttribute("new_notice").getValue();
   var Duration=Xrm.Page.getAttribute("new_durationinmonth").getValue();

   if(Notice != null && Notice != "" &&Duration != null && Duration != "" )
   {
 
     Notice.setMonth(Notice.getMonth() + Duration); 

   Xrm.Page.getAttribute("new_startdate").setValue(Notice);
     Xrm.Page.getAttribute("new_startdate").setSubmitMode("always");

     
   }

}

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: adding a decimal number for a date

    Thank you Aric And Wayne !

  • Aric Levin - MVP Profile Picture
    30,188 Moderator on at
    RE: adding a decimal number for a date

    So just use the months part:

    var numberOfMonths = Math.floor(duration);

    Notice.setMonth(Notice.getMonth() + numberOfMonths);

    This will only add 3 months instead of 3.5

    If you need to add 3.5, you will need to convert the 0.5 to 15 days as previously mentioned.

    Hope this helps.

  • Wayne Walton Profile Picture
    13,728 on at
    RE: adding a decimal number for a date

    If you don't have days, 3.5 months is correctly going to resolve to 1/July/2018.  Otherwise, Aric correctly gives you how you would need to parse that out to 16/July/2018.

  • Community Member Profile Picture
    on at
    RE: adding a decimal number for a date

    But I don't have days ! the duration is in month so when duration = 3 and notice 1/may/2018 --> start date= 1/july/2018 which is correct, but if duration = 3.5 its considered as the same case

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,188 Moderator on at
    RE: adding a decimal number for a date

    Hi Faith,

    You should consider changing your code so that it adds days to the months.

    For example, if you value is 3.5, you should use setMonth(month, days).

    Notice.setMonth(Notice.getMonth() + Duration);

    You can also use the Math.floor to get the number of days:

    var numberOfMonths = Math.floor(duration);

    var numberOfDays = (duration - Math.floor(duration)) * 30;

    Notice.setMonth(Notice.getMonth() + numberOfMonths, numberOfDays);

    Check your options here:

    www.w3schools.com/.../jsref_setmonth.asp

    Hope this helps.

  • Wayne Walton Profile Picture
    13,728 on at
    RE: adding a decimal number for a date

    You're going to have to parse the decimal into a number of days yourself and add the days to the date.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#2
Christoph Pock Profile Picture

Christoph Pock 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans