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

Chuck,



Thanks for the reply. We tried but it doesn't seem to allow us to convert datetime to date with glidedate


As chuck mentioned you can get date using GlideDate() API. Here is the sample code



var gd= new GlideDate();


gd.setDisplayValue(gs.nowDateTime());


gs.print(gd.getDisplayValue());


PaulHoogenboom
Tera Contributor

Hi Carlos,



I was struggling with this issue too. I got to this solution:


        var datefieldDate = datefield.toString().slice(0,10);



which results in this date representation: 2016-08-02


or


        var datefieldDate = datefield.getDisplayValue().toString().slice(0,10);



which results in this date representation: 02-08-2016



Regards,


Paul


samwallace881
Giga Expert

This is what I used and it worked in my scoped app:



var start_dates = new GlideDate();


start_dates = gs.quartersAgoStart(1);



var end_dates = new GlideDate();


end_dates = gs.quartersAgoEnd(1);



var real_start_date = start_dates.split(' ')[0];


var real_end_date = end_dates.split(' ')[0];



Then I returned the values.


ANINDYA SUNDAR
Tera Contributor

Hi @carlosrlara 

you can try this ...........

var gdt = new GlideDateTime("2015-06-20 04:00:00");

var date=gdt.getDate();

gs.print(date);