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

Notifications

Announcements

No record found.

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)
  • Wayne Walton Profile Picture
    13,730 on at

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

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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.

  • Community Member Profile Picture
    on at

    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

  • Wayne Walton Profile Picture
    13,730 on at

    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.

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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.

  • Community Member Profile Picture
    on at

    Thank you Aric And Wayne !

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans