Covert DateTime format to date only
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2016 06:12 AM
We are trying to compare two date to execute some function. However,the date time compare logic is off depends on the time of execution.
We would like to either zero out the time field or split the var into date only the split function doesn't seem to be functional
We have the following script
dateSection = dateSection.split(' ')[0]; //Gets the Date
var dateSection2 = new GlideDateTime();
dateSection2.addDaysUTC(-1*schd.frequency); //subtract frequency
dateSection2 = dateSection2.split(' ')[0]; //Gets the Date
var dateSectionComp = dateSection.compareTo(dateSection2);
var comp = last_run.compareTo(mark_date);
gs.log('Last run DateSection : ' + dateSection + ' DateSection2 Date : ' + dateSection2 + ' The comparison is : ' + dateSectionComp);
gs.log('Last run : ' + last_run + ' Mark Date : ' + mark_date + ' The comparison is : ' + comp);
The result should be the following
e,g
it should look like
from: 05/26/2016 12:34:56
and become the below
eg. 05/26/2016 00:00:00 or 05/26/2016
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2016 06:16 AM
dateSection.getByFormat('MM-dd-yyyy');
and
dateSection2.getByFormat('MM-dd-yyyy')
should work, instead of splitting.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2016 06:17 AM
Hi Carlos,
Did you try getDate method of GlideDateTime Class? Here is an example
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gs.print(gdt.getDate());
*** Script: 2011-08-31
Refer below link
GlideDateTime - ServiceNow Wiki
Thanks
Abhinandan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2016 06:42 AM
Abhinandan and Jim,
Thank you for the replies. I am trying both.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2016 06:20 AM
You may want to also take a look at the GlideDate() API, especially if you are dealing with scoped applications.