November 11, 2016

Date Manipulation in Siebel eScript

We have seen how to manipulate date values in Siebel Workflows. In this post we will see how can we work with date variables in Siebel eScript and use them to work with date fields.

I am not the author of these functions, It is just collection of useful functions that I have implemented in few projects. Please feel free to share your coding tricks for date variables in comments below.

Date Functions in Siebel eScript
Date Functions in Siebel eScript

How to add days to a date?


function AddToDate(sourceDate, nDays, nHours, nMinutes, nSeconds, nsign)
{
   // sourceDate  :  Date object
  // nDays, nHours , nMinutes , nSeconds  :  Integer numbers
  // nsign : Can have two values : 1 or ­1
  //             1 indicates to ADD to the sourceDate
  //            ­1 indicates to SUBTRACT from the sourceDate
  // Returns : A date object, after adding/subtracting
  // ndays,hNours,nMinutes
  //          and nseconds to the sourceDate.
  var retDate = sourceDate;
  retDate.setDate(retDate.getDate()+nsign*nDays);
  retDate.setHours(retDate.getHours()+nsign*nHours);
  retDate.setMinutes(retDate.getMinutes()+nsign*nMinutes);
  retDate.setSeconds(retDate.getSeconds()+nsign*nSeconds);
  return (retDate);
}

 How to find difference of days between two dates?

function DiffDays(date1, date2)
{// date1 : Date object
// date2 : Date object
// Returns : Number of days between date1 and date2
return ((date2.getTime()­ - date1.getTime())/(1000*60*60*24));
}

How to convert date object to String?

function DateToString(dDate)
{
  // dDate  :  Date object
  // Returns : A string with the format "mm/dd/yyyy" or "mm/dd/yyyy
hh:mm:ss"
  var sMonth = ToString(dDate.getMonth() + 1);
  if (sMonth.length == 1) {sMonth = "0" + sMonth;}
  var sDay = ToString(dDate.getDate());
  if (sDay.length == 1) {sDay = "0" + sDay;}
  var sHours = ToString(dDate.getHours());
  if (sHours.length == 1) {sHours = "0" + sHours;}
  var sMinutes = ToString(dDate.getMinutes());
  if (sMinutes.length == 1) {sMinutes = "0" + sMinutes;}
  var sSeconds = ToString(dDate.getSeconds());
  if (sSeconds.length == 1) {sSeconds = "0" + sSeconds;}
  if (sHours == "00" && sMinutes == "00" && sSeconds == "00")
    return (sMonth +"/"+  sDay +"/" + dDate.getFullYear())
  else
    return (sMonth +"/"+  sDay +"/" + dDate.getFullYear() +"
"+sHours+":"+sMinutes+":"+sSeconds);

}

  How to convert String to Date Object?


function StringToDate(sDate)
{
  // sDate  :  A string with the format "mm/dd/yyyy" or "mm/dd/yyyy
hh:mm:ss"
  // Returns : a Date object
  var ArDateTime = sDate.split (" ");
  var  ArDate = ArDateTime[0];
  var  splitDate = ArDate.split ("/");
  var nDay = ToNumber(splitDate[1]);
ar nMonth = ToNumber(splitDate[0]);
  var nYear = ToNumber(splitDate[2]);
  if (ArDateTime.length == 1)
     return (new Date(nYear, nMonth­1 , nDay))
  else
  {  var ArTime = ArDateTime[1];
      var splitTime = ArTime.split(":");
      if (splitTime[0]=="00" && splitTime[1]=="00" && splitTime[2]=="00"
)
            return (new Date(nYear, nMonth­1 , nDay))
      else
      {
            var nHours     = ToNumber(splitTime[0]);
            var nMinutes   = ToNumber(splitTime[1]);
            var nSeconds = ToNumber(splitTime[2]);
            return (new Date(nYear,nMonth­1,nDay, nHours, nMinutes,
nSeconds))
      }
   }
}

How to find records created in past 5 days? 

var dToday = new Date();
var FiveDaysAgo =  AddToDate(today,5,0,0,0,­1);
var bo = TheApplication().GetBusObject("Service Request");
var bc = bo.GetBusComp("Service Request");
bc.ActivateField("Description");
bc.ClearToQuery();
bc.SetViewMode(AllView);
bc.SetSearchSpec ("Created", ">= " + "'" + DateToString(FiveDaysAgo)
+ "'");
bc.ExecuteQuery();
TheApplication().RaiseErrorText(bc.CountRecords());

How to compare two dates?

function CompareDates(dte_from,dte_to)
{
/* Function to compare two dates.. will return 1 if dte_from is greater than dte_to else will return 0 */
var myM = ToInteger(dte_from.getMonth()+1);
var myD = ToInteger(dte_from.getDate());
var myY = ToInteger(dte_from.getFullYear());
var toM = ToInteger(dte_to.getMonth()+1);
var toD = ToInteger(dte_to.getDate());
var toY = ToInteger(dte_to.getFullYear());
if ((myY < toY)||((myY==toY)&&(myM < toM))||((myY==toY)&&(myM==toM)&&(myD < = toD)))
 {
  return(0);
 }
else
{
  return(1);
}
}

How to find if date is a future date?


function IsFutureDate(mydate)
{
/*
 *  Function to check if a date is greater than today
 *  Returns 0 if Current Date is larger
 *  1 if Passed Variable is larger
*/

var istoday = new Date();   
var myM = ToInteger(mydate.getMonth()+1);
var myD = ToInteger(mydate.getDate());
var myY = ToInteger(mydate.getFullYear());
var toM = ToInteger(istoday.getMonth()+1);
var toD = ToInteger(istoday.getDate());
var toY = ToInteger(istoday.getFullYear());
if ((myY < toY)||((myY==toY)&&(myM < toM))||((myY==toY)&&(myM==toM)&&(myD < = toD)))
 {
  return(0);
 }
else
{
  return(1);
}
}

** Code is provided for conceptual purpose only, please test the code explained throughly before implementing in production environment.