Results 1 to 3 of 3

Thread: Date Calculation!!!!

  1. #1
    Join Date
    Feb 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Date Calculation!!!!

    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>

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    dt = new Date();
    df = new Date( dt - 30*86400000);
    Code:
    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by ann
    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:

    Code:
    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:

    Code:
    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:

    Code:
    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:

    HTML Code:
    <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:

    Code:
    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:

    Code:
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •