Useful Date Functions

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Useful Date Functions

  • Comments 5
  • Likes

Occasionally you will need to fetch a date from either the CRM database or perhaps from an external system that is then needs to be formatted so it looks like a CRM date.

All users are able to set their date preferences. 

The are other articles that discuss handling dates

The functions show below can be used to format a date and cause it to be displayed according to the user preferences.  The example is based on usage in an ASP page but the code could be modified for other pruposes.

With out the usage of the function the retrieved dates would look like:

With the function the dates look correct


 

Functions

/////////////////////////////////////////////////////////////////////////
//
//         Leading zeros
//
//////////////////////////////////////////////////////////////////////////

function leadingZero(x)
{
//this examines a number to determine whether or not to add leading zeros.
//useful for dates
//example usage:
//var myDate = new Date();
//Response.Write(leadingZero(myDate.getDate()));
//
if (x <10)
return '0'+x;
else
return x;
}

////////////////////////////////////////////////////////////////////////
//
//          Formatting of Dates
//
//////////////////////////////////////////////////////////////////////////

function formatDate(x)
{
// This retrieves the users preferred dates and uses this to format returned dates.
//makes use of the leadingZero function

var myRecordId = eWare.GetContextInfo('user','user_userid');
var myRecord = eWare.FindRecord('usersettings',"uset_key like 'NSet_userdateformat%' and uset_userid="+myRecordId);
var sourceDate = new Date(x);

var gotDate = leadingZero(sourceDate.getDate())
var gotMonth = leadingZero(sourceDate.getMonth()+1);
var gotYear = sourceDate.getYear();
var gotHours = leadingZero(sourceDate.getHours());
var gotMinutes = leadingZero(sourceDate.getMinutes());
var gotTime = gotHours+':'+ gotMinutes;
var resultDate

switch(myRecord.uset_value)
{
case 'mm/dd/yyyy':
resultDate = gotMonth +'/'+ gotDate +'/' + gotYear +' '+gotTime;
//+ ' ' + sourceDate.getHours() +':' sourceDate.getMinutes()
return resultDate;

case 'dd/mm/yyyy':
resultDate = gotDate +'/' + gotMonth+'/'+ gotYear +' '+gotTime;
//+ ' ' + sourceDate.getHours() +':' sourceDate.getMinutes()
return resultDate;

case 'yyyy/mm/dd':
resultDate = gotYear +'/' + gotMonth +'/'+ gotDate +' '+gotTime  ;
//+ ' ' + sourceDate.getHours() +':' sourceDate.getMinutes()
return resultDate;

default:
resultDate = gotMonth +'/'+ gotDate +'/' + gotYear +' '+gotTime;
//+ ' ' + sourceDate.getHours() +':' sourceDate.getMinutes()
return resultDate;
}
}

Downloadable example

The functions have been used in an example rebuild of the Company Quicklook Page.  Development Partners can download the code from here

Comments
  • Here's how I do it. This works client side as well as server side:

    function ZPad(x, n) { return String(new Array(n+1).join("0") + x).slice(-1 * n); }

    function DateToString(dt) {

    var dateFormat = CurrentUser.user_prf.toLowerCase();

    var pos = 0; var dayPos = 0; var monthPos = 0; var yearPos = 0; var seperator=''; var dStr = '';

    for (i = 1; i < dateFormat.length; i++)

    switch (dateFormat.substr(i, 1)) {

    case 'd': dayPos = pos; break;

    case 'm': monthPos = pos; break;

    case 'y': yearPos = pos; break;

    default : if (seperator=='') seperator = dateFormat.substr(i, 1); pos++;

    };

    for (i = 0; i <= 2; i++) {

    switch (i) {

    case dayPos: dStr += ZPad(dt.getDate(),2); break;

    case monthPos: dStr += ZPad(dt.getMonth(),2); break;

    case yearPos: dStr += dt.getFullYear(); break;

    }

    if (i<2) dStr += seperator;

    }

    return dStr;

    }

  • Dates are a nightmare in general, dates in CRM seem to be just that much more so - so this is really good stuff. Thanks

  • Thanks Jeff, v useful. I trimmed it for just UK dates, and the assumption we are passing a date:

    function leadingZero(x)

    {

       //this examines a number to determine whether or not to add leading zeros.

       //useful for dates

       //example usage:

       //var myDate = new Date();

       //Response.Write(leadingZero(myDate.getDate()));

       //

       if (x

  • Hi Jeff,

    I checked the UserSettings table and it seems that the NSet_UserDateFormat are stored as "dd/mm/yyyy" instead of "dd/MM/yyyy" in the database even though I selected "dd/MM/yyyy".

    Regards,

    Leng Hong

  • Thanks for the tip!