Solved

Sage CRM Javascript Date Difference

Posted on 2011-03-24
6
1,404 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now