Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Date Functions

  1. #1
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts

    Date Functions

    I am working on a series of functions that I keep getting asked about. I finnaly thought I would code some usefull stuff and get away from the crap I have been coding.

    The following three scripts do this
    --Date Validation
    --Calculate Difference Between Dates
    --Date Addition or Subtraction

    I am sure that these have been done before, but I just filled the last 20 minutes by coding these. I only did a quick browser test, nothing extensive.

    Code:
    <script>
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //Date Validation
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      function DateValid(mo, dy, yr){
        TheDate = mo+"/"+dy+"/"+yr;
        Date1 = new Date(TheDate) 
        D=Date1.getDate();
        M=Date1.getMonth()+1;
        Y=Date1.getYear();
        if(mo!=M || D!=dy || yr!=Y){
            alert("Invalid Date Entered");
            return false;
        }
        else return true;
      }
      
      //DateValid( month , day , year )
      ralph = DateValid(1,13,2003)
      if(ralph)alert("valid date");
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //Calculate Difference Between Dates
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
       var MinM = 1000 * 60;   //X = 0 - Answer Minutes      
       var HrM = MinM * 60;    //X = 1 - Answer Hours
       var DyM = HrM * 24;     //X = 2 - Answer Days
       var WkM = DyM * 7;      //X = 3 - Answer Weeks
       var YrM = DyM * 365;    //X = 5 - Answer Years
       var MnM = YrM / 12;     //X = 4 - Answer Months
       
       var OutMill = new Array(MinM,HrM,DyM,WkM,MnM,YrM);
    
      function DayDiff(mo1, dy1, yr1, mo2, dy2, yr2, X){
        Date1 = Date.UTC(yr1, mo1 - 1, dy1) 
        Date2 = Date.UTC(yr2,mo2 - 1, dy2)   
        d = new Date();                              
        DateDiff = Date2 - Date1;
        Ans = Math.round(DateDiff / OutMill[X]);
        return(Ans);                       //Return difference.
      }
     
     
      //DayDiff( Month1 , Day1 , Year1 , Month2 , Day2 , Year2 , Number of Output Type )
      ralph = DayDiff(1,13,2003,5,13,2003,4)
      alert(ralph + " months")
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //Date Addition or Subtraction
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
      function DateAdd(mo, dy, yr, ma, da, ya){
        TheDate = parseFloat(mo+ma) +"/"+parseFloat(dy+da)+"/"+parseFloat(yr+ya);
        Date1 = new Date(TheDate) 
        D=Date1.getDate();
        M=Date1.getMonth()+1;
        Y=Date1.getYear();
        if(Y<1900)Y+=1900;
        TheUpDate= M + "/" + D + "/" + Y;
        return TheUpDate
      }
    
      //DateAdd(Month, Day , Year , Months to Add , Days to Add, Years to Add)
      ralph = DateAdd(1,13,2003,0,13,0)
      alert(ralph)
    
    </script>
    A1ien51

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #3
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,460
    Thanks
    1
    Thanked 23 Times in 21 Posts

    Cool

    My book, in Chapter Six (I think it's Six), adds a function to Date.prototype for telling you if you're on Daylight Savings time or not.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ya, I'd much rather see this as an object or a Date object extension
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”


  •  

    Posting Permissions

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