How do I convert glide date to integer date

samfindlay1
Giga Expert

I want to to use a group of records from pa_scores in a Map Page script,

and I want to return values where the start_to date is last month, however

start_to is an integer date value, so how do I convert a standard glide date

into an integer date like so:

var date = new GlideDate();

date.setValue(gs.beginningOfLastMonth());

// convert here ?

gs.print(date /*as an integer date i.e 29012016*/ );

1 ACCEPTED SOLUTION

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

You should be able to do like this:



var date = new GlideDate();  


date.setValue(gs.beginningOfLastMonth());  


var dateasint = date.toString().replace('-','');


gs.print(dateasint);



and if it need to "really" be a integer you can do like this:



var date = new GlideDate();  


date.setValue(gs.beginningOfLastMonth());  


var dateasint = parseInt(date.toString().replace('-',''));


gs.print(dateasint);


View solution in original post

8 REPLIES 8

Uncle Rob
Kilo Patron

Working with date objects makes my inner child cry.


This is the wiki page I always go to when I have to work with them:   GlideSystem Date and Time Functions - ServiceNow Wiki



Lacking finesse in this area I'd probably do something awful like extract the day, month, and year as strings, concatenate them, and then make the resulting string an integer.


williamsun
Mega Guru

Hey!


This is what you are looking for: gs.print(date.getNumericValue());


For other good glide date stuff look in these places:


GlideDateTime - ServiceNow Wiki


GlideSystem Date and Time Functions - ServiceNow Wiki


Scoped GlideDate API Reference - ServiceNow Wiki


Using Date and Time Fields - ServiceNow Wiki


Well numericvalue gives the numbers of seconds from 1970(?).



As I understand it he wants the date to be without the -     so instead of 2016-01-29 he wants 20160129 (well in a different format for mm:dd:yy I guess)


Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

You should be able to do like this:



var date = new GlideDate();  


date.setValue(gs.beginningOfLastMonth());  


var dateasint = date.toString().replace('-','');


gs.print(dateasint);



and if it need to "really" be a integer you can do like this:



var date = new GlideDate();  


date.setValue(gs.beginningOfLastMonth());  


var dateasint = parseInt(date.toString().replace('-',''));


gs.print(dateasint);