Finding a serial number of a day.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Udara chathuranga
    New Member
    • Aug 2010
    • 14

    Finding a serial number of a day.

    what is the java script source code to find the serial number of a date of a year.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    may i ask what a 'serial number of a date of a year' is? never heard of it - could you give an example or link to it?

    Comment

    • Udara chathuranga
      New Member
      • Aug 2010
      • 14

      #3
      The serial number of 1900.01.01 is 1.
      The serial number of 1901.01.01 is 365.
      Serial number is the system assigned number for a date from the day 1900.01.01

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        in that case it is basically the day difference to the base date. a simple way could be to extend the JavaScript date object like this:
        Code:
        Date.prototype.getSerialDateNumber = function() {
            var baseDate = new Date(1900,1,1);
            
            // one day based on ms
            var oneDay = 86400000;
        
            var dayDiff = Math.ceil(
                (this.getTime() - baseDate.getTime()) / oneDay
            );
        
            return dayDiff;
        }
        
        var d = new Date;
        
        alert(d.getSerialDateNumber());
        Last edited by gits; Aug 23 '10, 01:46 PM.

        Comment

        • Udara chathuranga
          New Member
          • Aug 2010
          • 14

          #5
          Code:
          <html><head><title>Day counter</title>
          <script language="javascript">
             Date.prototype.getSerialDateNumber = function() {
                 var baseDate = new Date(1900,1,1);
          	   //this is the date that i want to find the serial number--------I add this
          	var mydate=new Date(1900,1,31);
              // one day based on ms
              var oneDay = 86400000;
          
          
                 var dayDiff = Math.ceil(
                   (mydate.getTime() - (baseDate.getTime()-oneDay)) / oneDay 
          		 //"oneDay" should be abstracted from baseDate.getTime() to get the right answer isn't it...
              );
          
           
               return dayDiff;
          
           }
            
            var d = new Date;
            
           alert(d.getSerialDateNumber());
           
          </script>
          </head>
          <body>
          </body>
          </html>
          Thank you very much sir, I understood your code and I have done some changes On it. Is it a good change or a bad one. Please reply. How ever thank you very much for your help.
          Last edited by gits; Aug 25 '10, 08:20 PM. Reason: fix code tags

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            it works for this case ... but you wouldn't have needed it ... in line 21 of your last code post the following would have done the job:

            Code:
            var d = new Date(1900,1,31);
            your change fixes the output to a specific date and makes the date-extension nearly useless ... which could give you the serial date number for any date without your change - so ... you might judge your change for yourself ... what would you say? bad or good change? :)

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              ahh ... and we need a fix for the baseDate:

              Code:
              new Date(1900,1,1)
              
              // is: Thu Feb 01 1900 00:00:00 GMT+0100 (CET)
              
              // while:
              
              new Date(1900,0,1)
              
              // is: Mon Jan 01 1900 00:00:00 GMT+0100 (CET)
              so the month starts at 0 for january ... just overlooked it before :) you might have a look here for a reference to the JavaScript Date object.
              Last edited by gits; Aug 25 '10, 09:45 PM.

              Comment

              • Udara chathuranga
                New Member
                • Aug 2010
                • 14

                #8
                Thanks, Now your code is working properly. Please be in touch.

                Comment

                Working...