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

Yes. Your ngModel is still using the "data" object which is from the server side.

Change ng-model="data.valid_for" to ng-model="c.valid_for"

 

Edit:

setting_date_c_object.png

Thanks @ChrisBurks  - For providing solution.

Can we  hardcode any other date( in place of today's date , e.g. 31-DEC-2055)

Yes. The Date object can take an argument.

new Date('31-DEC-2005');