Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

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

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

  • james.fairless Profile Picture
    james.fairless 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

  • james.fairless Profile Picture
    james.fairless 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

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 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)

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans