1. Computing & Technology

Guide's Date and Time Scripts

Your guide has written a number of Javascripts that can be easily added to any site to add date and time or timer displays. Just follow the supplied instructions to install the script. No programming experience is required.

Number of Tuesdays This Month

Not necessarily Tuesdays but any day of the week. You know that there are either four or five of a given day of the week in any given month but which is it. This simple script will give you the answer.

Julian Date

At first glance it appears that the Javascript Date object doesn't have methods for converting to and from Julian dates. In fact such conversions can be easily done with the addition of the day of year method and a couple of lines of code.

Month and Day Names

We can easily add getMonthName() and getDateName() methods to JavaScript Date objects to make retrieving the month and day of the week names easier.

Julian Day Date Method

Adding a new method to Date objects to allow us to get the Julian Day is a relatively simple task (if you know the right numbers to use in the calculation).

Changing When the Week Starts

Not everywhere in the world start their week on the same day. JavaScript treats the week as starting on Sunday while the week itself originally started on Saturday and some places still start it then while some other places have moved the start day to Monday. An extra simple JavaScript Date method can adjust the day numbers that JavaScript returns to handle this.

Week of Year Date Method

Sometimes the methods provided for the standard Javascript objects and classes don't include the ones you are looking for. The solution is to build your own. Here we look at how to add a getWeek() method that can be used with date objects.

Day Difference and Business Day Difference

Determining the number of days or business days between two dates is relatively straightforward when you know how but difficult if you don't. These two methods added to all your dates will allow you to get the answers without needing to write new code each time.

Daylight Saving Time

JavaScript does not directly have access to a flag indicating if your visitor is on daylight saving time or not but if we assume that if their location uses daylight saving time that it only happens for a part of the year over summer we can add a couple of extra methods to the Date object to work it out.

Add Business Days to a Date

Adding days to a date is relatively straightforward using a date object. Adding business days isn't quite so straightforward unless you add a method to handle the complications for you.

Add Days to a Date Method

You can perform all sorts of complex manipulations on dates by using the appropriate date methods. To save you having to remember how to code these combinations you can create your own date methods for performing common tasks. Here is the code to add a method to make adding and subtracting days easier.

Swatch Beats

The people who created the Swatch also brought us a version of internet time known as Beats. Here's a method to add to your date objects to retrieve the current beat value.

Standard Week Number

The International Standards Organisation have a definition for which days belong to which week of what year. Here are methods to add to your dates to obtain that value.

Date Array Lookup

A simple function can take a date in MMDD format and find the first entry in an array that is on or after that date.

Date Array Sort

Sorting an array of dates in yyyy/mm/dd order is simple. That doesn't mean that sorting an array of dates in mm/dd/yyyy or dd/mm/yyyy order is hard. You just need to set up the right compare routine.

Last Day of Month

There are several ways to determine how many days there are in a given month. Some are shorter than others but some don't always work.

On This Day

When you want to provide different processing for each day of the year you need to know what day it is. This function extracts the current date in MMDD format for you.

Date Last Modified

Type a simple Javascript command into the browser address bar to check how recently a web page was last updated.

Day Selection

Using three dropdown lists for date entry and only having the right number of days display for the month already selected is easy with this JavaScript.

Calendar Generator

Do you want to add a calendar for a specific month to your web page? Creating all of the HTML by hand can be a daunting task. This simple form allows you to enter the month and year and which day of the week you want the calendar to start with and it will generate the HTML for you.

Age Last Birthday

This Date method allows you to compare two dates to accurately determine how old someone was/is on a specific date.

Date Format Method

One thing that the JavaScript Date object lacks is an easy way of producing formatted date and time outputs the way that some other languages provide via a simple format command. Well now you can add your own format method that uses the same commands to control the formatting as PHP does.

Date Methods

Here is a collection of date methods that can be added to your web page to allow you to manipulate dates far more easily.

Event Calendar

This script places a calendar on the web page. When your visitors select a date from the calendar an associated event for that date is displayed in the area below the calendar.

Countdown Timer

Place a timer on your web page that counts down to a specific time at your location.

Improved Author Time Script

This version of my author time script adds the ability to optionally display the date as well in either international or US format. It also no longer uses either document.write or innerHTML and instead uses the proper DOM calls to insert the text into the web page.

Display Author Time

The author of a web page need not be in the same time zone as the server on which the site is hosted. This script allows the author to display the time at their location on the page regardless of where the site is hosted or where their visitors are provided that the visitors have their computer's time set correctly for their location.

Seasonal Backgrounds

There are four seasons in the year. You can display a different background image on your page for each of these seasons by incorporating this simple script. You just need to let the script know whether to display Northern hemisphere seasons or Southern hemisphere seasons.

Analog Clock

Lots of people have digital clocks on their web page. With this script you can go one better with an analog clock instead. The script provides for lots of different customizations so you can probably find a way to get it to display how you want.

Multiple Analog Clocks

The one thing that you can't do with my original Analog Clock script is to put two or more clocks on the same page. This version of the script allows you to do that.

Daily Background

Just a couple of lines of JavaScript are sufficient to place different background images on your page for each day of the month.

©2012 About.com. All rights reserved.

A part of The New York Times Company.