Thursday, 23 January 2014

231: A 40K Imperial Date Blogger Widget

  • Have you ever wondered what todays date was in the Imperial Calendar?
  • Are you ever writing a battle report or bit of fluff and want to include some Imperial Dates but don't know how it works?
  • Are you bored right now and interested in seeing a little bit of javascript?
If you answered "yes" to any of the above, read on, if not, well this isn't the post for you - sorry.

So the other day I got curious as to whether it would be possible to produce a little widget on my blog that would display the current date, but in the Imperial Calendar.  I've done some light web design and programming before, so I thought it should be within my ability, and if not, then I could always stitch together other bits of code to do what I wanted (which by the way, is pretty much how all code is produced :D ).  In case you hadn't noticed, if you look at the top of my blog on the right, I met with some success and was able to produce just such a widget.

As an offering to the community, in the same way that Dezartfox from The-Vanus-Temple did with his rotating image tutorial, I thought I'd share the code with you all in case you would like to add a similar widget to your blogs.  To add this, all you need to do is insert a new html/javascript widget into your blog, and copy and past the code below into the box, simple as that.



 <!DOCTYPE html>  
 <html>  
 <body>  
   
 <script>  

 /* created by iron-legion.blogspot.co.uk 2014-01-17 */
 /* http://iron-legion.blogspot.co.uk/2014/01/231-40k-imperial-date-blogger-widget.html */
   
 var now=new Date();  
 var year=now.getFullYear();  
 var hour=now.getHours();  
   
 function daysInFebruary(year) {  
   if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {  
     // Leap year  
     return 29;  
   } else {  
     // Not a leap year  
     return 28;  
   }  
 }  
   
 function dateToDay(date) {  
   var feb = daysInFebruary(date.getFullYear());  
   var aggregateMonths = [0, // January  
               31, // February  
               31 + feb, // March  
               31 + feb + 31, // April  
               31 + feb + 31 + 30, // May  
               31 + feb + 31 + 30 + 31, // June  
               31 + feb + 31 + 30 + 31 + 30, // July  
               31 + feb + 31 + 30 + 31 + 30 + 31, // August  
               31 + feb + 31 + 30 + 31 + 30 + 31 + 31, // September  
               31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30, // October  
               31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, // November  
               31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, // December  
              ];  
   return aggregateMonths[date.getMonth()] + date.getDate();  
 }  
   
 function padToThree(number) {  
   if (number<=999) { number = ("00"+number).slice(-3); }  
   return number;  
 }  
   
 days=dateToDay(now);  
 yearFraction = ((days*24)+hour)/8.766;  
 padFraction=padToThree(yearFraction.toFixed(0))  
   
 padYear=year.toString().slice(1);  
   
 document.writeln(0,".",padFraction,".",padYear,".M03")  
   
 </script>  
   
 </body>  
 </html>  

Code added to the post using http://codeformatter.blogspot.co.uk/
 - Andrew

6 comments:

  1. Excellent - I love it, thanks for sharing! Cheers!

    ReplyDelete
    Replies
    1. You are welcome. Zzzzzzz, mentions he can't get it to work, as the other commenter, have you had a chance to try it, if I've made a mistake I'd need to fix it.

      Delete
  2. Interesting. I couldn't make it work, but then that's nothing new...

    ReplyDelete
  3. Hmmmm, hope i've copied it down right, are you putting it in an HTML/JavaScript widget? If so, are there any error messages given. If I've got something wrong, I hope to find and fix it. Cheers

    ReplyDelete
  4. Thanks for sharing that! I have wanted something like this for a long while! I got it to work I think, will have to check to see if the date changes tomorrow! Ha! Thanks again for sharing this!

    ReplyDelete
    Replies
    1. I'm really glad it worked for you, and happy to be able to share it with others. Do let me know if it updates, it should change about every 8 hours or so.

      Delete

LinkWithin

Related Posts Plugin for WordPress, Blogger...