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 :
Customer experience | Sales, Customer Insights,...
Answered

Javascript help set Date Time Field set hours Javascript

(0) ShareShare
ReportReport
Posted on by

Hey I am experimenting with JS onLoad to attempt to resolve an Issue I have. 

The purpose is to set the Hour to 1am onLoad and onChange.

After I get this work I also plan to hide to the time but that is not included in this JS yet.


Here is the JS:

function updateHours(executionContext) {
//Get the context of the form
var formContext = executionContext.getFormContext();
// Access the field on the form
var StartFieldAttribute = formContext.getAttribute("test_startdate");
var EndFieldAttribute = formContext.getAttribute("test_enddate");




if(StartFieldAttribute.getValue()!= null)
{ var StartFieldValue = StartFieldAttribute.getValue();
var StartFieldValue = StartFieldValue.setHours(1, 0, 0);
StartFieldAttribute.setValue(StartFieldValue);
}

if(EndFieldAttribute.getValue()!= null)
{
var EndFieldValue = EndFieldAttribute.getValue();
var EndFieldValue = EndFieldValue.setHours(1, 0, 0);
EndFieldAttribute.setValue(EndFieldValue);
}


}

here is it in plain text:

function updateHours(executionContext) {
//Get the context of the form
var formContext = executionContext.getFormContext();
// Access the field on the form
var StartFieldAttribute = formContext.getAttribute("test_startdate");
var EndFieldAttribute = formContext.getAttribute("test_enddate");




if(StartFieldAttribute.getValue()!= null)
{ var StartFieldValue = StartFieldAttribute.getValue();
var StartFieldValue = StartFieldValue.setHours(1, 0, 0);
StartFieldAttribute.setValue(StartFieldValue);
}

if(EndFieldAttribute.getValue()!= null)
{
var EndFieldValue = EndFieldAttribute.getValue();
var EndFieldValue = EndFieldValue.setHours(1, 0, 0);
EndFieldAttribute.setValue(EndFieldValue);
}


}



Here is the error message:

Value should be of type: Date: Parameter Name: value

Event Name: onload

Function Name: updateHours

Any help is appreciated

I have the same question (0)
  • Verified answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Replace

    var StartFieldValue=StartFieldValue.setHours(1,0,0);

    With

    var StartFieldValue=new Date(StartFieldValue.setHours(1,0,0)):

    Same change for EndfieldValue

  • AJZafar Profile Picture
    on at

    This is the answer i really appreciate your help

  • Bipin D365 Profile Picture
    28,983 Moderator on at

    Please mark my answer verified by clicking yes under Did this answer your question?

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans