Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


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!


Related HTML, JavaScript and Web Site Programming articles:
✔   How to Create Predefined Google Image Search Links?
Thanks for the Amazon URL [see Creating Amazon Search Links]. That worked beautifully. In fact, I sent you $5.00 for coffee in thanks....
✔   Can I embed a Facebook search box on my blog site?
I've seen your articles about how to add a Twitter or Google search box on a Web page, but I have a tougher...
✔   Can I use CSS for drop shadows on my blog?
I want to give my site a bit of a facelift and add some neat graphical elements. One of which is drop shadows....
✔   How can I embed interactive photo panoramas on my site/blog?
I read through your blog entry about how to take panoramic photos with iOS 6 and an iPhone 5 and got enthused. I've...
✔   How can I create a Twitter search URL shortcut?
I'd like to add a few Twitter search links to my Web site. Is that possible, or does Twitter prohibit this sort of...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  









Reader Comments To Date: 1

kirti chaudhary said, on April 24, 2007 10:33 PM:

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)...

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

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











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 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. Further, 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. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.