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
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Location
    Breda, Netherlands
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question change date formatting

    Hi,

    I am not much of a coder, but I managed to adapt glenngv's birthdayscript to an eventscript wich displays the events for the next 9 days. And it works fine.
    I have a question of a cosmetic nature. The date format is mm / dd / yyyy. I would like to change that to dd.mm.yy
    I can't figure out how.
    Can somebody help me? This is the current code:
    Code:
    <script type="text/javascript">
    Date.prototype.getDiff = function(date, interval){
      if (typeof date == "string"){
         date = new Date(date);
      }
      if (isNaN(date) || !(date instanceof Date)){
         return NaN; //invalid date passed
      }
      if (typeof interval == "undefined") interval = "ms"; //msec (default)
      var diff = this - date; //alert(this+' - '+date+" = "+diff)//diff in msec
      switch(interval.toLowerCase()){
        case "s": //sec
          diff = diff/1000; break;
        case "n": //min
          diff = diff/(1000*60); break;
        case "h": //hr
          diff = diff/(1000*60*60); break;
        case "d": //day
          diff = diff/(1000*60*60*24); break;
        case "m": //month
          diff = diff/(1000*60*60*24*30); break;
        case "y": //year
          diff = diff/(1000*60*60*24*365); break;
        default:
          ; //msec
      }
      return Math.floor(diff);
    }
    
    var arrBday = [
      ['event1','8/27/2005'],
      ['event2','8/27/2005'],
      ['event3','8/28/2005'],
      ['event4','8/28/2005'],
      ['event5','8/30/2005'],
      ['event6','9/10/2005']
    ];
    
    function getBdayList(numDays){
      var bday,temp,idx,diff;
      var today = new Date();
      var bdayList = new Array();
      for (var i=0;i<arrBday.length;i++){
        bday = new Date(arrBday[i][1]);
        if (isNaN(bday)) continue;
        temp = new Date(today.getFullYear(), bday.getMonth(), bday.getDate(), 23, 59, 59, 999);
        diff = temp.getDiff(today, "d");
        if (diff >= 0 && diff <= numDays){
           idx = bdayList.length;
           bdayList[idx] = new Object();
           bdayList[idx].name = arrBday[i][0];
           bdayList[idx].bday = arrBday[i][1];
    
        }
      }
      return bdayList;
    }
    
    function displayBdayList(){
      var bdayList = getBdayList(7);
      var len = bdayList.length;
      var s = "";
      if (len>0){
        for (var i=0; i<len; i++){
           if (s != ""){
              s += " " + bdayList[i].bday + " - " + bdayList[i].name + " <br />"
           }
           else{
              s = bdayList[i].bday + " - " + bdayList[i].name + " <br />"
           }
         }
      }
      else{
        s = "geen activiteiten.";
      }
      document.write(s);
    }
    </script>
    </head>
    <body>
    <h1>Agenda</h1>
    <script type="text/javascript">
    displayBdayList();

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,031
    Thanks
    0
    Thanked 247 Times in 244 Posts
    See the changes in red.
    Code:
    function getBdayList(numDays){
      var bday,temp,idx,diff;
      var today = new Date();
      var bdayList = new Array();
      for (var i=0;i<arrBday.length;i++){
        bday = new Date(arrBday[i][1]);
        if (isNaN(bday)) continue;
        temp = new Date(today.getFullYear(), bday.getMonth(), bday.getDate(), 23, 59, 59, 999);
        diff = temp.getDiff(today, "d");
        if (diff >= 0 && diff <= numDays){
           idx = bdayList.length;
           bdayList[idx] = new Object();
           bdayList[idx].name = arrBday[i][0];
           bdayList[idx].bday = bday.getDate().padZero() + "." + (bday.getMonth()+1).padZero() + "." + bday.getFullYear().toString().substr(2,2);  //dd.mm.yy
        }
      }
      return bdayList;
    }
    
    Number.prototype.padZero = function(){
      var num = (this<10) ?  "0" + this : "" + this;
      return num;
    }

  • #3
    New to the CF scene
    Join Date
    Aug 2005
    Location
    Breda, Netherlands
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up thanks!

    thanks a lot!
    you're forever in my cool book

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,031
    Thanks
    0
    Thanked 247 Times in 244 Posts
    Glad to be of help to you.


  •  

    Posting Permissions

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