Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Date & Time Control in CRM 2016

(0) ShareShare
ReportReport
Posted on by 135

I have a question regarding the CRM 2016 Date & Time control. Currently, when I click on any time drop down it selects 8:00 AM as the default time. I would like to change this behaviour, so when I click on any time drop down, it selects the current time instead of 8:00 AM.  Is there any way to accomplish this task through Javascript? Any help is well appreciated.  

 

Thanks. 

*This post is locked for comments

  • yleclerc Profile Picture
    yleclerc 1,547 on at
    RE: Date & Time Control in CRM 2016

    Any ideas someone? I am trying to get current time by defaut, but leaving the ability to modify if necessary.

  • yleclerc Profile Picture
    yleclerc 1,547 on at
    RE: Date & Time Control in CRM 2016

    OK, thanks Bharat! I will give this a try.

    Also, do you think I could try to allow manual change once the field has value in it? So when I select a date, the current time gets populated. But once there is value, the script stops firing onchange.

    Something like this:

    function dateAndTimeControl_OnChange() {
    var d = Xrm.Page.getAttribute("actualend").getValue();
    var dtNow = new Date();
    var hour = dtNow.getHours();
    var min = dtNow.getMinutes();
    var newdt = d.setHours(hour,min,0);
       if (d == null) {
       Xrm.Page.getAttribute("actualend").setValue(newdt);
       }
       else { }
    }

    I tried it, but the code doesn't fire at all.

    Thanks for your help!

  • BharatPremji Profile Picture
    BharatPremji 2,485 on at
    RE: Date & Time Control in CRM 2016

    Hi Yvan

    Not sure if this will fit your requirement but how about creating a two option field (or optionset depending on your requirement) that the user would set if they wanted the date/time set to now.

    For example:  If they two option field was called Set Date/Time Now, and the user set it to Yes, you could fire the code, if not the user could manually set the date/time.  You could even take it a step further by locking the field and setting the date/time to Now if they user ticks the box.

    Regards

    Bharat

  • yleclerc Profile Picture
    yleclerc 1,547 on at
    RE: Date & Time Control in CRM 2016

    I have one more question for Barhat :)

    The code works fine, thanks!

    Now, what if I want to default the time to the current time, but keep the ability to adjust it manually?

    With provided code, it defaults to current time, but if I change the time manually, it still comes back to current time.

    Thanks!

  • CRM-Dev Profile Picture
    CRM-Dev 135 on at
    RE: Date & Time Control in CRM 2016

    Thanks Bharat, this has resolved my issue.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Date & Time Control in CRM 2016

    Hi,

    Also,you will get the formtype using  Xrm.Page.ui.getFormType();

    //Onload you can use the below snippet/////

    var formType = Xrm.Page.ui.getFormType();

    if (formType == 1) {

        Xrm.Page.getAttribute("FIELD_NAME").setValue(Date.now());

           }

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Date & Time Control in CRM 2016

    Hi CRM-Dev,

    Through javascript code,onload, you can check whether your form type is create or update type, if formtype=1 then Xrm.Page.getAttribute("FIELD_NAME").setValue(Date.now()) and if its already updated formtype will be 2 then the field remain as is!

    Regards,

    Neethuanna Mathew

  • Verified answer
    BharatPremji Profile Picture
    BharatPremji 2,485 on at
    RE: Date & Time Control in CRM 2016

    Hi,

    Apologies, i misunderstood your requirement.

    Try something like this on change of your Date & Time Control:

    function dateAndTimeControl_OnChange() {
    var d = Xrm.Page.getAttribute("date&TimeControl").getValue();
    var dtNow = new Date();
    var hour = dtNow.getHours();
    var min = dtNow.getMinutes();
    var newdt = d.setHours(hour,min,0);
    Xrm.Page.getAttribute("date&TimeControl"").setValue(newdt);

    }

  • CRM-Dev Profile Picture
    CRM-Dev 135 on at
    RE: Date & Time Control in CRM 2016

    My date and time field is already set to user local.

  • CRM-Dev Profile Picture
    CRM-Dev 135 on at
    RE: Date & Time Control in CRM 2016

    Hi Bharat,

    Unfortunately, your solution will not work.

    1. If I apply your solution to all my date fields on load, all my fields will have the current date and time values before even the user gets to pick anything. Plus null is a valid date and time value

    2. If I apply your solution to all my date fields on change, then the user will not get to choose any other dates other than today's date. The code will reset the value back to today's date every time  :)

    Here is how I needed it to work.

    Step 1. Pick a date:

    1524.Untitled2.png

    Step 2. Here is what the user will see after picking the date

    1524.Untitled2.png

    Step 3. Here is what I wan the user to see after picking the date

    5238.Untitled3.png

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,187 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,966 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans