Search code examples
google-apps-scriptdate-comparison

Issues with Date comparison Google apps script


Date comparison doesn't work correctly in the following situation. I am comparing set of two dates. first one works perfectly fine, having issues with second pair comparison:

function doGet(e){
var app = UiApp.createApplication();
var grid = app.createGrid(4, 3).setStyleAttributes({'margin':'20'});
grid.setWidget(0, 0, app.createLabel('User ID:'));
grid.setWidget(0, 1, app.createTextBox().setName('idBox').setId('idBox'));
grid.setWidget(1, 0, app.createLabel('From Date:'));
grid.setWidget(1, 1, app.createTextBox().setName('sDate').setId('sDate'));
grid.setWidget(2, 0, app.createLabel('To Date:'));
grid.setWidget(2, 1, app.createTextBox().setName('eDate').setId('eDate'));
createDatePicker(app);//date picker function works perfectly fine(used for selecting dates from calendar)

var dhandler = app.createServerHandler('pickDate');
app.getElementById('sDate').addClickHandler(dhandler);
app.getElementById('eDate').addClickHandler(dhandler);


var panel = app.createVerticalPanel().setId('settingsPanel');

panel.add(app.createLabel('Enter UserID:');

panel.add(grid);

var button = app.createButton('Search').setId('search');
var handler = app.createServerClickHandler('_searchUser');
handler.addCallbackElement(grid);
button.addClickHandler(handler);

grid.setWidget(3, 1, button);
app.add(panel);

return app;
}


function _searchUser(e){
var app = UiApp.getActiveApplication();

var id = e.parameter.idBox;
var sdt = Utilities.formatDate(new Date(e.parameter.sDate), "PST",                   'M/d/YYYY').toString();
var edt = Utilities.formatDate(new Date(e.parameter.eDate), "PST", 'M/d/YYYY').toString();

 app.add(app.createLabel("ID entered: "+id));
 app.add(app.createLabel("Start Date: "+sdt));
 app.add(app.createLabel("End Date: "+edt));



 var tsdt = Utilities.formatDate(new Date("6/1/2014"), "PST", "M/d/YYYY");//just for example
 var tedt = Utilities.formatDate(new Date("6/3/2014"), "PST", "M/d/YYYY");//just for example
 app.add(app.createLabel("Sample Start Date compared to: "+tsdt));
 app.add(app.createLabel("Sample End Date Compared to: "+tedt));

 if(sdt == tsdt){app.add(app.createLabel("Start Date: EQUAL "))}
 if(sdt > tsdt){app.add(app.createLabel("Start Date: Greater "))}
 if(sdt < tsdt){app.add(app.createLabel("Start Date: Smaller "))}

 if(edt == tedt){app.add(app.createLabel("End Date : EQUAL "))}
 if(edt > tedt){app.add(app.createLabel("End Date: Greater "))}
 if(edt < tedt){app.add(app.createLabel("End Date: Smaller "))}
 return app;
 }

The start dates in above code is compared perfectly, works for all cases i.e EQUAL to, Greater than and Smaller than. But the End Dates(edt and tedt) doesn't return the correct result for values of date that greater than one week to the sample date which is 6/3/2014. Which means if 6/10/2014 or any other dates till the end of the month is entered, then it returns Smaller as the result which should not be the case.

Can anybody please help me with this


Solution

  • Working with dates in Apps Script can be confusing sometimes. What you do need to know is work with the date first in normal javascript, then use the Utility to only format the date for a look. The issue here is the math is much easier in milliseconds which is what .getTime() gives you for a give date, from 1/1/1970 there is plenty to read on that later.

    Here is a sample that works

    function _searchUser(e){
    
    //var id = e.parameter.idBox;
    var sdt = new Date('6/1/2014').getTime();
    var edt = new Date('6/15/2014').getTime();
    
    
     var tsdt = new Date("6/1/2014").getTime()//just for example
     var tedt = new Date("6/3/2014").getTime()//just for example
    
     if(sdt == tsdt){Logger.log("Start Date: EQUAL ")}
     if(sdt > tsdt){Logger.log("Start Date: Greater ")}
     if(sdt < tsdt){Logger.log("Start Date: Smaller ")}
    
     if(edt == tedt){Logger.log("End Date : EQUAL ")}
     if(edt > tedt){Logger.log("End Date: Greater ")}
     if(edt < tedt){Logger.log("End Date: Smaller ")}
     }