PDA

View Full Version : Date Calculation!!!!



ann
02-28-2006, 06:26 PM
Hi,
I would like to get some help regarding the functions here.
As you can see this returns 2 dates: datefrom as 30 days from today(dateto).
Instead, I need the dates as the first day of the previous month and the
last day of the previous month as datefrom and dateto.

Please suggest......


<script>
var cntlName;
function right(str, n){
if (n <= 0)
return "";
else if (n > String(str).length)
return str;
else {
var iLen = String(str).length;
return String(str).substring(iLen, iLen - n);
}
}
function customCheckPage(){
var par1;
var par2;
for( var i=0; i<preProcessControlArray.length; i++){
cntlName = eval(preProcessControlArray[i]);
if ( cntlName.m_oSubmit.name.toLowerCase() == 'p_pardatefrom' ){
eval('pickerControl' + cntlName.m_sRef + '.lostFocus()');
par1 = cntlName.m_oForm.value;
}
if ( cntlName.m_oSubmit.name.toLowerCase() == 'p_pardateto' ){
eval('pickerControl' + cntlName.m_sRef + '.lostFocus()');
par2 = cntlName.m_oForm.value;
}
}
if (par1<=par2)
promptButtonFinish();
else
alert('FROM DATE parameter must be smaller than or equal to TO DATE parameter!');
}
for( var i=0; i<pageNavigationObserverArray.length; i++){
cntlName = eval( pageNavigationObserverArray[i] );
if(cntlName.m_oParent.onclick.toString().indexOf('promptButtonFinish()')>0 ){
cntlName.m_oParent.onclick = customCheckPage;
}
}
for( var i=0; i<preProcessControlArray.length; i++){
cntlName = eval(preProcessControlArray[i]);

dt = new Date();
df = new Date( dt - 30*86400000);

if (cntlName.m_oSubmit.name.toLowerCase() == 'p_pardatefrom' ){
cntlName.m_oEditBox.value = df.getFullYear()
+ '-' + right('0'+(1+df.getMonth()),2)
+ '-' + right('0'+df.getDate(),2);
eval('pickerControl' + cntlName.m_sRef + '.lostFocus()');
}
if (cntlName.m_oSubmit.name.toLowerCase() == 'p_pardateto' ){
cntlName.m_oEditBox.value = dt.getFullYear()
+ '-' + right('0'+(1+dt.getMonth()),2)
+ '-' + right('0'+dt.getDate(),2);
eval('pickerControl' + cntlName.m_sRef + '.lostFocus()');
}
}
</script>

Twey
02-28-2006, 07:09 PM
dt = new Date();
df = new Date( dt - 30*86400000);

var dt = new Date(), df = new Date();
dt.setTime(Date.parse(dt.getMonth() + "/01/" + dt.getFullYear());
df.setTime(Date.parse(((df.getMonth() == 0 ? 11 : df.getMonth() - 1) + 1) + "/01/" +(df.getFullYear() - (dt.getMonth() == 0 ? 1 : 0)));
That ought to do it. Untested.

mwinter
03-01-2006, 02:30 PM
Instead, I need the dates as the first day of the previous month and the last day of the previous month as datefrom and dateto.I haven't looked at Twey's suggestion in detail, but string manipulation is definitely unnecessary for calculation:



var now = new Date(),
year = now.getFullYear(),
month = now.getMonth(),
fromDate = new Date(year, month - 1, 1),
toDate = new Date(year, month, 0);
The (well-defined) out-of-range behaviour of the Date object and its methods will perform any and all necessary adjustments to create a valid date. Incidentally, this will fail in NN4 (though not IE4 or Op6): it doesn't seem to like date values less-than or equal-to zero and will return the first day for the current month (instead of the preceeding day). However, I can't imagine that you'll have any great need to support that dinosaur.

As a suggestion, consider using:



String.prototype.pad = function(l, c) {
var S = String(this);

while (S.length < l) S = c + S;
return S;
};
Number.prototype.pad = function(l, c, r) {
return this.toString(r || 10).pad(l, c || '0');
};
Date.prototype.toISODateString = function() {
return this.getFullYear()
+ '-' + (this.getMonth() + 1).pad(2)
+ '-' + this.getDate().pad(2);
};
which defines a new method for all Date instances, toISODateString (as well as two utility methods). I think you might agree that:



element.value = fromDate.toISODateString();
for example, looks much more readable than the code you have at present. If not, then at least consider the pad methods defined above (as an alternative to your right function).


<script>The type attribute is required:



<script type="text/javascript">

cntlName = eval(preProcessControlArray[i]);
if ( cntlName.m_oSubmit.name.toLowerCase() == 'p_pardatefrom' ){
eval('pickerControl' + cntlName.m_sRef + '.lostFocus()');All of these instances of the eval function are unnecessary. Without knowing what preProcessControlArray (and pageNavigationObserverArray) are, I can't suggest an alternative, but the other instance above (and similar occurrences elsewhere) are easily replaced:



global['pickerControl' + cntlName.m_sRef].lostFocus();
where global is a reference to the global object. You could use window for this, but I prefer to define my own reference:



var global = this;
The eval function should rarely ever be used. Certainly, the vast majority of all of its uses are in error and can be replaced by simpler, faster, and more robust alternatives.

Mike