Solved

Sage CRM Javascript Date Difference

Posted on 2011-03-24
6
1,414 Views
Last Modified: 2012-05-11
Trying to determine the number of months between two dates in Sage CRM. If the difference is more than 6 months, I need to notify the user.  CRM stores the date as "mm/dd/yyyy", but the js Date() function returns "Thu Mar 24 12:37:56 CDT 2011" (for example).  So with the two different formats, this doesn't work.

<script language="javascript">
window.attachEvent('onload', ShowMyAlert);

function ShowMyAlert()
{
  if(!document.getElementById("comp_name"))
  {
    var myDate = document.getElementById('_Datacomp_info_upd_ox').innerText;

   var myToday=new Date();

   var myMsg = "Company Record Needs Verification";

   var myDiff = dateDiff(myToday,myDate);

   if(myDiff > 180)
{
    alert(myMsg);
}
  }
}

function dateDiff(d1,d2) {
   diffMilli = d2.getTime()-d1.getTime();
   return diffMilli/(1000*60*60*24)
}

</script>
0
Comment
Question by:BowTech
  • 3
  • 2
6 Comments
 
LVL 7

Assisted Solution

by:nbove
nbove earned 200 total points
ID: 35209955
I think the problem is that your "myDate" variable is a string and not a date.  Trying modifying the declaration of your myDate variable to the following:
var myDate = new Date(document.getElementById('_Datacomp_info_upd_ox').innerText);

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 300 total points
ID: 35212054
@nbove is right, another one :

function ShowMyAlert() {
		if(!document.getElementById("comp_name")) {
			var myDate = document.getElementById('_Datacomp_info_upd_ox').innerText;
			
			myDate = myDate.split("/");
			myDate = new Date(myDate[2], myDate[0]-1, myDate[1]);

			var myToday = new Date();
			var myMsg = "Company Record Needs Verification";
			var myDiff = dateDiff(myToday,myDate);
			if(myDiff > 180) {
				alert(myMsg);
			}
		}
	}

Open in new window

0
 

Author Comment

by:BowTech
ID: 35218342
Thanks for the responses, but neither of these appears to catch the date. I'm wondering if the dateDiff function is out of scope (I'm not familiar with Javascript).

<script language="javascript">
window.attachEvent('onload', ShowMyAlert);

function ShowMyAlert()
{
  if(!document.getElementById("comp_name"))
  {
      var myDate = document.getElementById('_Datacomp_info_upd_ox').innerText;
                  
      myDate = myDate.split("/");
      myDate = new Date(myDate[2], myDate[0]-1, myDate[1]);

      var myToday = new Date();
      var myMsg = "Company Record Needs Verification";
      var myDiff = dateDiff(myToday,myDate);
      if(myDiff > 180) {
            alert(myMsg);
      }
  }
}

function dateDiff(d1,d2)
{
   diffMilli = d2.getTime()-d1.getTime();
   return diffMilli/(1000*60*60*24)
}


</script>
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:BowTech
ID: 35218408
Ah, I stand corrected. The result is negative, so I'll need to accommodate that.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 35218454
Yes,

      if(myDiff > 180) {
            alert(myMsg);
      }
      else {
           alert("<0");
      }

Open in new window


some good links (don't forget to vote for the first one) :
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_484-Can-I-have-a-date.html
http://www.w3schools.com/jsref/jsref_obj_date.asp
0
 

Author Comment

by:BowTech
ID: 35218556
I just changed the polarity of the dateDiff return value.  We're good. Thanks all!


function dateDiff(d1,d2)
{
   diffMilli = d2.getTime()-d1.getTime();
   return -diffMilli/(1000*60*60*24)
}

</script>
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Salesforce.com’s Console is a great tool to view activities, leads, contacts, accounts and opportunities all in one screen. It is particularly effective during call blocks and working numerous activities at a time in a quick, repetitive fashion (suc…
Note: You must have administrative privileges in order to create/edit Sharing Rules. Salesforce.com (http://www.salesforce.com) (SFDC) is a cloud-based customer relationship management (CRM) system. It is a database most commonly used by sales an…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question