- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2014 04:02 AM
I'm trying to look at a date range (valid from - valid to) and if today's date is within the range i want it to return true
my code up to now:
var ifTrue = false; var todaysdate = gs.now(); //if todays date is more than or equal to the from date and todays date is less than or equal to the to date return true if (todaysdate >= current.u_valid_from && todaysdate <= current.u_valid_to){ ifTrue = true; }
Logically this should work but it isn't and I was wondering if anyone knew of a better way to do it, maybe using a built in function/glide-script?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2014 11:37 AM
This script should do it for you! I tested it and it worked for me.
var todaysdate = gs.now();
var valid_from = new GlideDateTime(current.u_valid_from);
var valid_to = new GlideDateTime(current.u_valid_to);
//if today's date is more than or equal to the from date and today's date is less than or equal to the to date, return true
var inRange = (todaysdate >= valid_from && todaysdate <= valid_to)? true: false;
if (inRange){
//do your thing, yo
}
Did this help you?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2014 09:05 AM
You need to convert the date/times into numbers using getNumericValue() for the data/time objects. Something like this but its going to depend on your data types for the fields so you may need to force them to GlideObject/GlideDateTime first then get the numeric value.
- if (todaysdate.getNumericValue() >= current.u_valid_from.getNumericValue() && todaysdate.getNumericValue() <= current.u_valid_to.getNumericValue()){
- ifTrue = true;
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2014 11:37 AM
This script should do it for you! I tested it and it worked for me.
var todaysdate = gs.now();
var valid_from = new GlideDateTime(current.u_valid_from);
var valid_to = new GlideDateTime(current.u_valid_to);
//if today's date is more than or equal to the from date and today's date is less than or equal to the to date, return true
var inRange = (todaysdate >= valid_from && todaysdate <= valid_to)? true: false;
if (inRange){
//do your thing, yo
}
Did this help you?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2014 02:25 AM
Hi Casey
Still not working but i think this is close
My Printed results show the date values in different formats:
Todays Date 07-11-2014 10:24:33
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2014 02:41 AM
Hi Neil,
Modify Casey's code in this way and see if it works.
- var todaysdate = new GlideDateTime(gs.now()+" 00:00:00").getDisplayValue();
- var valid_from = new GlideDateTime(current.u_valid_from).getDisplayValue();
- var valid_to = new GlideDateTime(current.u_valid_to).getDisplayValue();
- //if today's date is more than or equal to the from date and today's date is less than or equal to the to date, return true
- var inRange = (todaysdate >= valid_from && todaysdate <= valid_to)? true: false;
- if (inRange){
- //do your thing, yo
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2014 03:36 AM
adding the display value to the end made it work thanks