Covert DateTime format to date only

carlosrlara
Mega Expert

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

9 REPLIES 9

jimnicholson
Giga Guru

dateSection.getByFormat('MM-dd-yyyy');


and


dateSection2.getByFormat('MM-dd-yyyy')



should work, instead of splitting.


Abhinandan Pati
Giga Guru

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


Abhinandan and Jim,



Thank you for the replies. I am trying both.


Chuck Tomasi
Tera Patron

You may want to also take a look at the GlideDate() API, especially if you are dealing with scoped applications.



ServiceNow Developers