what is the java script source code to find the serial number of a date of a year.
Finding a serial number of a day.
Collapse
X
-
Tags: None
-
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.01Comment
-
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
-
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.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>Comment
-
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:
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? :)Code:var d = new Date(1900,1,31);
Comment
-
ahh ... and we need a fix for the baseDate:
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.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)
Last edited by gits; Aug 25 '10, 09:45 PM.Comment
-
Comment