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 365 | Integration, Dataverse...
Suggested answer

Java script or other option to set the time part of a date/time field

(0) ShareShare
ReportReport
Posted on by 5

Hello,

I am looking for a solution which will allow me to set the time part of a date/time filed in Dynamics 365, does anyone know if thats possible?

Thanks in advance

I have the same question (0)
  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at
    RE: Java script or other option to set the time part of a date/time field

    Hi,

    Thank you for your query.

    Assuming your field is Date & Time, following is sample JS code:

    function setTimePart(executionContext) {
    
        const formContext = executionContext.getFormContext();
    
        var reminderDateTime = formContext.getAttribute("new_remindat").getValue();
    
        if (reminderDateTime) {
    
            reminderDateTime.setHours(10);
            reminderDateTime.setMinutes(30);
            // Sets time part to 10:30
            formContext.getAttribute("new_remindat").setValue(reminderDateTime);
        }
    
    }
     

    For more details on JS Time manipulation please refer to:

    JavaScript setTime() Method (w3schools.com)

  • james.fairless Profile Picture
    5 on at
    RE: Java script or other option to set the time part of a date/time field

    Thank you so much for the quick reply however the script generated the error below

    TypeError: Cannot read property 'getFormContext' of undefined

    I used the script exactly as you has it above with the only exception of changing the ("new_remindat") parts to be ("new_enddate") to match the field name on my form

    I would appreciate anymore help you could offer.

    Thanks

  • james.fairless Profile Picture
    5 on at
    RE: Java script or other option to set the time part of a date/time field

    Hello again,

    My mistake I forgot to tick the "pass execution context as first parameter" checking that allows the script to work.

    Is it possible to use a similar script to add X hours to the date time filed in the same way?

    Thanks

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Sahan Hasitha Profile Picture

Sahan Hasitha 231

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 83 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 68 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans