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

Announcements

No record found.

News and Announcements icon
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,985 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,985 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 180 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 123

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans