Converting JavaScript date to Sage CRM date

By | April 10, 2020

Hello everyone! In Sage CRM date fields helps user to track when a particular record is been created or updated. Some additional date fields like closing date on opportunity or Due date on communication plays vital role in defining business logic and escalation as well.

New Stuff: Sage CRM’s Data loading issue on Mobile browser

Consider a business scenario where an opportunity target close date should be set to after 3 months from opportunity open date. For example if I create an opportunity with open date say 1 Jan, 2020 then target close date should be set to 1 April, 2020. Which might be pretty simple when you follow the below steps.
1. Read value of Opportunity open date
2. Add 3 months with the current date value
3. Store the Calculated date into Closed by field.

//Read value of opportunity open date field
var sOpenDate =new Date($('#oppo_opened').val());
//add some months to the date
var ClosedDate= new Date(sOpenDate.setMonth(sOpenDate.getMonth()+3));
$("#oppo_targetclose").val(ClosedDate);
Invalid Date Format
Invalid Date Format

But this will still not allow user to Save the opportunity record instead display validation message related to Incorrect Date format.

Validation Message
Validation Message

This is because we have not added anything to handle the date format. Hence, in our blog we will explain how you can add Date Format. Just go ahead and add the below function to you ClientFunc.js.

function FormatDate(pmDate)
{
dateFormat = crm.CurrentUser.userpref_dateformat.toLowerCase();
console.log(dateFormat);

var mm = pmDate.getMonth() + 1;
            var dd = pmDate.getDate();
            var year = pmDate.getFullYear();

if (dateFormat == "mm/dd/yyyy")
{
	if (mm < 10)
	{
		if (dd < 10)
		{
			FormattedDate = "0"+mm + "/" + "0" + dd + "/" + year;
		}
		else
		{
			FormattedDate = mm+"/" + "0" +dd + "/" + year;
		}

	}
	else
	{
		FormattedDate = mm + "/" + dd + "/" + year;
	}
}
if (dateFormat == "mm.dd.yyyy")
{
	if (mm < 10)
	{
		if (dd < 10)
		{
			FormattedDate = "0"+mm + "." + "0" + dd + "." + year;
		}
		else
		{
			FormattedDate = mm+"." + "0" +dd + "." + year;
		}
	}
}
else
	{
		FormattedDate = mm + "." + dd + "." + year;
	}

if (dateFormat == "dd.mm.yyyy")
{
	if (mm < 10)
	{
			if (dd < 10)
			{
				FormattedDate = "0"+dd + "." + "0" +mm + "." + year;
			}
			else
			{
				FormattedDate = dd+"." + "0" +mm+ "." + year;
			}
	}
}
else
{
	FormattedDate = dd + "." + mm + "." + year;
}
if (dateFormat == "dd/mm/yyyy")     //dd/MM/yyyy
{
	if (mm < 10)
	{
		if (dd < 10)
		{
			FormattedDate = "0"+ dd + "/" + "0" + mm + "/" + year;
		}
		else
		{
			FormattedDate = dd + "/" + "0" + mm + "/" + year;
		}
	}
	else
	{
		FormattedDate = dd + "/" +  mm + "/" + year;
	}
}
return FormattedDate;
}

Next, update your code to include the above date format function.

//Read the Opportunity open Date 
var sOpenDate =new Date($('#oppo_opened').val());
//add some months to the date
var ClosedDate= new Date(sOpenDate.setMonth(sOpenDate.getMonth()+3));
ClosedDate= FormatDate(ClosedDate);
$("#oppo_targetclose").val(ClosedDate);	

Now you can see that Closed By field value is set in Standard Sage CRM format, refer below screenshot:

Correct Date Format
Correct Date Format

Now, you can handle any date format by using the function. Hope this information helps.

About Us

Greytrix – a globally recognized and one of the oldest Sage Development Partner is a one-stop solution provider for Sage ERP and Sage CRM organizational needs. Being acknowledged and rewarded for multi-man years of experience, we bring complete end-to-end assistance for your technical consultations, product customizations, data migration, system integrations, third party add-on development and implementation competence.

Greytrix helps in upgrading Sage ERP and CRM to the latest version and provides migration for Sage Intacct from Sage 300, Sage 100, Sage 50 and QuickBooks; for Sage 100, Sage 300 (Sage Accpac), Sage X3 from Sage Pro, Sage 50 US, Sage 50 CA, Sage 50 US, Sage Business Works, Sage Business Vision and QuickBooks; for Sage CRM from Salesforce | ACT! | SalesLogix | Goldmine | Sugar CRM | Maximizer

For more information on Sage ERP – CRM migration solutions, please contact us at erpmig@greytrix.com. We will like to hear from you.