Computing Data From 2 Dates

[Total: 0    Average: 0/5]

This example shows how to take two dates and calculate a value based upon the delta day count.

Body Code:

<SCRIPT type="text/javascript">
var _m = "2" // march 
var _d = "15" 
var _y1 = "2000" 
var _d2 = new Date() // current date 
var _d1 = new Date() 
    _d1.setMinutes(0)
	_d1.setSeconds(0) 
    _d1.setMonth(_m) 
    _d1.setDate(_d) 
    _d1.setYear(_y1) 
function DaysDelta(_v1,_v2) { 
  var _delta = 0; 
  var _a1 = _v1; 
  var _a2 = _v2; 
  if (_v1.getTime() < _v2.getTime()) { 
    _a1 = _v2; 
    _a2 = _v1; 
  } 
  _delta = _a1.getTime() - _a2.getTime(); // in msecs  but what is the accuracy?
  //document.write("<BR>a1-time="+_a1.getTime()+"<BR>a2-time="+_a2.getTime()+"<BR>");
  _delta /= (1000 * 60 * 60 * 24); // days 
  //_delta = Math.round(_delta); // ADDED THIS TO GET EXACT DAY COUNT
  return _delta; 
} 
document.write("Start Date   :"+_m+"/"+_d+"/"+_y1+"<BR>") 
document.write("Today's Date : "+_d2.getMonth()+"/"+_d2.getDate()+"/"+_d2.getYear()+"") 
var _X = 4.5 // in thousands! 
var _d = DaysDelta(_d1,_d2); 
var _v = _d * _X * 1000;
    _v *= 1000; //back into units of dollars
	_v = Math.round(_v);
//document.write('<BR>Delta Days = '+_d);  // commented out
document.write('<BR><BR>New Dollar Figure=$'+printFancy(_v)+"<BR>"); 

function printFancy(_d) {
  var _s = _d.toString();
  var _a = new Array();
  var _r = "";
  while (_s.length>0) {
    _a[_a.length?_a.length:0] = _s.substring(_s.length-3,_s.length);
    _s = _s.substring(0,_s.length-3);
  }
  for (var i=_a.length-1;i>=0;i--) {
    _r += _a[i]+",";
  }
  _r = _r.substring(0,_r.length-1);
  return _r;
}
</SCRIPT>

Times Viewed: 4

Leave a Reply

Your email address will not be published. Required fields are marked *