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.

Problems with Date and daysAgo()

Mikolz
Kilo Contributor

Hey everyone, having issues with getting the contents of a glide date field and getting the date from 7 days ago.


var date = current.week_starts_on;
var thisTime = date.getGlideObject().toString();
var time = thisTime.setValue(gs.daysAgo(7));
gs.addInfoMessage(time);


Just for testing purposes I am using the info message. I am always getting an error. I think the error is coming from the gs.daysAgo() method. If I remove it and place the variable thisTime in the info mesage it is returning. Seems like everytime I try to work with a datefield there is some special technique that needs to be used...thanks for reading!
3 REPLIES 3

cwilker10
Giga Expert

No need for setValue(). Just set time = gs.daysAgo(7);

Or I'm just confused as to what you're trying to do.


Mikolz
Kilo Contributor

Thanks for the response, I will explain...So I have a date on a specific form that will always fall on a sunday. I want to take that current date and go back 7 days which would give me last weeks entry. Ultimately, I am going to use the information in glide record.


Mikolz
Kilo Contributor

Gotta love working with glide date and glide date time fields. This gave me what I needed, if anyone cares...



var CurWeekStart = current.week_starts_on.getGlideObject();
var useThisTime = new GlideDateTime(CurWeekStart);

useThisTime.addWeeks(-1);
var finalTime = useThisTime.getDate();

gs.addInfoMessage(finalTime);


SO if the current week on the form is 12-02-2012, this will give me 2012-11-25. Just what I needed...