Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Set default date in input field for widget

Ujjwal1
Tera Contributor

Hi ,

I need to set today's date as default date for input ( i can be  modified by user), in widget. 

Please suggest.

 

 

  <div>
    <input type="date" id="valid_for" name="valid_for" ng-model="data.valid_for">
  </div>

 

 

 

1 ACCEPTED SOLUTION

ChrisBurks
Giga Sage

According to the AngularJS docs : https://docs.angularjs.org/api/ng/input/input%5Bdate%5D , the date must be in the form of a Date object (javascript date object).

If you're setting it with a ServiceNow date object (GlideDate), it won't work because it's not the same as a regular javascript date object.

In this case it can be set from the client side using "new Date()";

<input class="form-control" type="date" id="valid_for" name="valid_for" ng-model="c.valid_for">

 

Client controller

api.controller=function() {
  /* widget controller */
  var c = this;
	c.valid_for = new Date();
};

 

setting_date.png

 

View solution in original post

7 REPLIES 7

imajad
Tera Contributor

I think you can use AngularJS expression like this 

otherwise I think you can leverage the server side scripts using - GlideDate

<div>
    <input type="date" id="valid_for" name="valid_for" ng-model="data.valid_for" 
           ng-init="data.valid_for = (new Date()).toISOString().split('T')[0]">
</div>

Ujjwal1
Tera Contributor

hi @imajad 
Thanks for response. But it did not work. 

ChrisBurks
Giga Sage

According to the AngularJS docs : https://docs.angularjs.org/api/ng/input/input%5Bdate%5D , the date must be in the form of a Date object (javascript date object).

If you're setting it with a ServiceNow date object (GlideDate), it won't work because it's not the same as a regular javascript date object.

In this case it can be set from the client side using "new Date()";

<input class="form-control" type="date" id="valid_for" name="valid_for" ng-model="c.valid_for">

 

Client controller

api.controller=function() {
  /* widget controller */
  var c = this;
	c.valid_for = new Date();
};

 

setting_date.png

 

hi @ChrisBurks 
Thanks for response.

I am not getting date in preview. Am i missing something here ? 

Ujjwal1_0-1723553623294.png