Solved

Sage CRM Javascript Date Difference

Posted on 2011-03-24
6
1,430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
replacing inline javascript with jquery 4 63
object method as an event handler 8 73
How can I do this in Angular, instead of jQuery? 2 22
Natural height and jquery 6 38
Note: You must have administrative privileges in order to configure lead or case queues. Salesforce.com (http://www.Salesforce.com) is a cloud-based customer relationship management (CRM) system. It is widely used around the world by sales and ma…
Read about achieving the basic levels of HRIS security in the workplace.
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…

737 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