How to convert Date and time into date

Prathamesh Cha1
Tera Contributor

Hi all,

Can anyone tell me why the dates are unequal and how can I convert this dateTime into time so that the both dates will be equal.

 

         var val = '';
          var date = new GlideRecord('question_answer');
          date.addQuery('sys_id', '725fb14c1b9a31d01eeafc07cb4bcb42')
          date.query();
          if(date.next()){
               val = date.value;
               gs.print('expected date')
               gs.print(val)
               var gdt = new GlideDateTime(val);
               gdt.getDate();
               gdt.addDays(-14);
               gs.print('final expected date')
               gs.print(gdt)
                var td = new GlideDate();
               gs.print('today date')
               gs.print(td)
               if(gdt==td){
                gs.print('equal')
               }
                }
 

 

PrathameshCha1_0-1698675653400.png

 

 

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Prathamesh Cha1 Update your script as follows.

 

var val = '';
          var date = new GlideRecord('question_answer');
          date.addQuery('sys_id', '725fb14c1b9a31d01eeafc07cb4bcb42')
          date.query();
          if(date.next()){
               val = date.value;
               gs.print('expected date')
               gs.print(val)
               var gdt = new GlideDateTime(val);
               gdt.getDate();
               gdt.addDays(-14);
               gs.print('final expected date')
               gs.print(gdt)
                var td = new GlideDate();
               gs.print('today date')
               gs.print(td)
               if(gdt.getDate().getDisplayValue()==td.getDisplayValue()){
                gs.print('equal')
               }
                }

Hope this helps.

View solution in original post

2 REPLIES 2

Samaksh Wani
Giga Sage
Giga Sage

Hello @Prathamesh Cha1 

 

You are comparing todays date with the date which is 14 days prior, so it will come up as unequal only.

 

secondly for extract time from your gdt variable.

 

add this line of code.

 

var arr = gdt.split(" ");

gs.info(arr[1]);

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

Sandeep Rajput
Tera Patron
Tera Patron

@Prathamesh Cha1 Update your script as follows.

 

var val = '';
          var date = new GlideRecord('question_answer');
          date.addQuery('sys_id', '725fb14c1b9a31d01eeafc07cb4bcb42')
          date.query();
          if(date.next()){
               val = date.value;
               gs.print('expected date')
               gs.print(val)
               var gdt = new GlideDateTime(val);
               gdt.getDate();
               gdt.addDays(-14);
               gs.print('final expected date')
               gs.print(gdt)
                var td = new GlideDate();
               gs.print('today date')
               gs.print(td)
               if(gdt.getDate().getDisplayValue()==td.getDisplayValue()){
                gs.print('equal')
               }
                }

Hope this helps.