Ramblings and Thoughts Related to Software and Website Development. I Moderate This Blog With An Iron Fist. There Will Be No Mercy!
Windows Ticks from Javascript Date object.
Published on April 16, 2008 By andrew_ In Software Development

During my quest to add more features to the forums on the Stardock Network of websites, I came upon the need for a decent DateTimePicker control for ASP.NET. I tested out several javascript generated datetime pickers/choosers and just wasnt happy with what I found. Aside from the code-style of the js, or the dependancy on some framework, none of them were wrapped up nicely in a server control I could re-use easily.

My solution? Write my own. It took me about a day to put together some easy to read, compact js that generated a display control for the 'picker' result (displaying the date and time) and a nice popup with a dynamic calendar and time control (ala. Windows' Date Time applet). I had the server control written and ready to go, to the point where I was ready to handle posted data. That is, data which I needed to use on postback.

Enter the nightmare. I figured I would use Ticks as output. DateTime strings and how theyre formatted aren't consistent across platforms, and can cause exceptions when trying to parse them. Additionally, if someone used GreaseMonkey or some such to modify the result, they could cause said exceptions. Ticks are ticks. If someone manages to modify the result value then it at the least, wont cause an exception. Ticks are just numbers. Huzzah.

Oh. Crap. Javascript doesnt do ticks. (Not surprising, as js is intended to be cross-platform. Ticks, how .NET handles them, are Windows only)


The result from all of this is a nice, compact prototyping for the Date object. The code is available on my personal site via the 'External Link' for download, and below;

Code: javascript
  1. Date.prototype.ticks = function(date)
  2. {
  3.    this.day = date.getDate();
  4.    this.month = date.getMonth() + 1;
  5.    this.year = date.getFullYear();
  6.    this.hour = date.getHours();
  7.    this.minute = date.getMinutes();
  8.    this.second = date.getSeconds();
  9.    this.ms = date.getMilliseconds();
  10.   
  11.    this.monthToDays = function(year, month)
  12.    {
  13.       var add = 0;
  14.       var result = 0;
  15.         if((year % 4 == 0) && ((year % 100  != 0) || ((year % 100 == 0) && (year % 400 == 0)))) add++;
  16.         
  17.       switch(month)
  18.       {
  19.          case 0: return 0;
  20.          case 1: result = 31; break;
  21.          case 2: result = 59; break;
  22.          case 3: result = 90; break;
  23.          case 4: result = 120; break;
  24.          case 5: result = 151; break;
  25.          case 6: result = 181; break;
  26.          case 7: result = 212; break;
  27.          case 8: result = 243; break;
  28.          case 9: result = 273; break;
  29.          case 10: result = 304; break;
  30.          case 11: result = 334; break;
  31.          case 12: result = 365; break;
  32.       }
  33.       if(month > 1) result += add;
  34.       return result;     
  35.    }
  36.    this.dateToTicks = function(year, month, day)
  37.    {
  38.       var a = parseInt((year - 1) * 365);
  39.       var b = parseInt((year - 1) / 4);
  40.       var c = parseInt((year - 1) / 100);
  41.       var d = parseInt((a + - c);
  42.       var e = parseInt((year - 1) / 400);
  43.       var f = parseInt(d + e);
  44.       var monthDays = this.monthToDays(year, month - 1);
  45.       var g = parseInt((f + monthDays) + day);
  46.       var h = parseInt(g - 1);
  47.       return h * 864000000000;
  48.    }
  49.    this.timeToTicks = function(hour, minute, second)
  50.    {
  51.       return (((hour * 3600) + minute * 60) + second) * 10000000;
  52.    }  
  53.   
  54.    return this.dateToTicks(this.year, this.month, this.day) + this.timeToTicks(this.hour, this.minute, this.second) + (this.ms * 10000);
  55. }

Comments
on Apr 18, 2008

I'm moderating out all the silly comments, sorry guys. My development posts need to keep a serious tone.