Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

How can I use Javascript and CSS to display the date?

I received this email from a younger reader: "Dave, thanks for your cool book Cool Web Sites, but I still need a bit of help. I want to have a little box on my page that shows the person the current date, like you'd see on a day-a-page calendar. I figure you can do that in CSS and Javascript, but I can't figure it out. Can ya help me?"

Dave's Answer:

You're right, this is a job for Javascript, to get the current date and extract the fields desired, and Cascading Style Sheets, to format it in a nice manner.

Let's start with the Javascript snippet. To get the current date in Javascript, you need to create a date object, then access its specific attributes to identify individual items. Here's what I mean:

<script language="javascript" 
  type="text/javascript">
var today = new Date();
That gets you the current date in "today". The two elements we're interested in are the month name and the day of the month number. Unfortunately, the Javascript object has the month number, not the month name, so we're going to have to use a simple array to map number to name. But it's not too bad:
var monthnum = today.getMonth();
var monthname=new Array("JAN","FEB","MAR","APR","MAY",
  "JUN","JUL","AUG", "SEP","OCT","NOV","DEC");
var day=today.getDate();
</script>
Now we have the month number in monthnum and the day of the month in day. They can be output as part of this Javascript snippet thusly: document.write(monthname[monthnum], day) but that doesn't make it easy to add the CSS formatting we need. Instead, let's switch to the CSS for a minute, then we'll go back to how we can actually use the variables we've instantiated.

To have this solution be as portable as possible, I have created a small CSS block that contains three new classes, the overall "outerbox" of the date, the "datebox" with the number, and the "monthbox" with the month name. Here's how they look:

<style type="text/css" rel="stylesheet">
.outerbox { border:1px solid black;padding:0px;
    width:50px;
    overflow:hidden;margin:4px; font-weight:900;
    font-family: verdana,arial,sans-serif; }
.datebox  { font-size:22pt;color:#666;text-align:center;
    border-bottom:1px solid black; }
.monthbox { font-size:11pt;color:#fff;
    background-color:#666; text-align:center; }
</style>
Now, if you've studied CSS, you'll be able to look at this and see what's going on.

The outerbox has a 1 pixel black border, no internal padding, a fixed width of 50 pixels, and a 4 pixel margin. It also will chop off any text inside that won't fit in the box (the "overflow:hidden") so that the date box doesn't get too wonky if someone experiments with very large type. Finally, the outerbox specifies a sans-serif typeface, in bold.

The datebox has 22pt dark gray text, centered, and adds a 1 pixel black line separating the date and the month name. The monthbox is basically the same, using a font size that's half of datebox and inverted colors (that is, white text on a dark gray background).

Put these all together and here's the entire CSS + Javascript + HTML sequence, at once:

<style type="text/css" rel="stylesheet">
.outerbox { border:1px solid black;padding:0px;
    width:50px;
    overflow:hidden;margin:4px; font-weight: 900; 
    font-family: verdana,arial,sans-serif; }
.datebox  { font-size:22pt;color:#666;text-align:center;
    border-bottom:1px solid black; }
.monthbox { font-size:11pt;color:#fff;
    background-color:#666;text-align:center; }
</style>

<script language="Javascript" type="text/javascript">
var today = new Date();
var monthnum = today.getMonth();
var monthname=new Array("JAN","FEB","MAR","APR","MAY",
   "JUN","JUL","AUG","SEP","OCT","NOV","DEC");
var day=today.getDate();
</script>

<div class="outerbox">
<div class="datebox">

<script language="Javascript" type="text/javascript">
document.write(day)
</script>

</div>
<div class="monthbox">

<script language="Javascript" type="text/javascript">
document.write(monthname[monthnum])
</script>

</div>
</div>
And the result, since I know you're dying to see it:


Pretty neat result, I think!

I hope that's what you were seeking when you wrote to me, but if not, I'm sure that this will give you a good start on whatever you are trying to accomplish!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

I have a date 20070414 (ccyymmdd) , i wanted to convert the numeric value of the month into its name ie April instead of 04 (i need in unix).
My question is how can i get the month name from its numeric value (in unix)...

Posted by: kirti chaudhary at April 24, 2007 10:33 PM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]