The CreatorCon Call for Content is officially open! Get started here.

script to set the date in a field to prior day

giovanni3
Kilo Contributor

Any javascript gurus out there that can provide a script that would set the default date in a "date" field for the prior day?

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

If you want to take the time zone into consideration, set the default value to:



For a Date field:


javascript:var gdt = new GlideDate();gdt.addDays(-1);gdt.getDisplayValue();



For a DateTime field:


javascript:var gdt = new GlideDateTime();gdt.addDays(-1);gdt.getDisplayValue();



The "getDisplayValue()" is the trick if you want to use the current user's time zone.   Otherwise you will get the date in GMT, which could be an issue.


View solution in original post

6 REPLIES 6

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Giovanni,



You can set the dictionary default value as javascript:gs.daysAgoStart(1);


Please check section 2.15 in below wiki link for more details.


http://wiki.servicenow.com/index.php?title=GlideSystem_Date_and_Time_Functions#daysAgo.28int.29


Jim Coyne
Kilo Patron

If you want to take the time zone into consideration, set the default value to:



For a Date field:


javascript:var gdt = new GlideDate();gdt.addDays(-1);gdt.getDisplayValue();



For a DateTime field:


javascript:var gdt = new GlideDateTime();gdt.addDays(-1);gdt.getDisplayValue();



The "getDisplayValue()" is the trick if you want to use the current user's time zone.   Otherwise you will get the date in GMT, which could be an issue.


Thanks for the quick response Jim!   That second reply worked like a charm.   You and all the rest of the people that support us non-java developers that have to support this platform is what makes this community so invaluable. I hope you continue to do what you do so well.   Thanks again!


You are welcome and thanks for the kind words.   The Community is definitely a great place to learn and I enjoy giving back whenever I can.



I'll ask bianca.vaccarini to change this discussion to a question so that you could mark the appropriate response as the "Correct Answer".   This helps others know that a valid answer was given for your question.