Dates
Answers to frequently asked questions on how to use dates with Javascript.
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.
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.
JavaScript Dates
The Date Object in JavaScript makes manipulating and displaying dates and times really easy. It works even better if you add further methods to it.
The Date Object in JavaScript makes manipulating and displaying dates and times really easy. It works even better if you add further methods to it.
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.
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.
Date Last Modified
Type a simple Javascript command into the browser address bar to check how recently a web page was last updated.
Type a simple Javascript command into the browser address bar to check how recently a web page was last updated.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Validating Dates
Before you do anything with a date that your visitors have input, you first need to check that it is a valid date. In order to then perform any processing on the date entered you will then need to convert it to a date object. This function will take a date entered seperately as year, month, and day and will both validate that it is a valid date and if it is will return the date as a date object.
Before you do anything with a date that your visitors have input, you first need to check that it is a valid date. In order to then perform any processing on the date entered you will then need to convert it to a date object. This function will take a date entered seperately as year, month, and day and will both validate that it is a valid date and if it is will return the date as a date object.
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.
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.
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.
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.
Age Last Birthday
This Date method allows you to compare two dates to accurately determine how old someone was/is on a specific date.
This Date method allows you to compare two dates to accurately determine how old someone was/is on a specific date.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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).
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).
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.
We can easily add getMonthName() and getDateName() methods to JavaScript Date objects to make retrieving the month and day of the week names easier.
Day of Year Date Method
Another addition to the date object, this one allows you to retrieve the number of a day within the year (sometimes known as the Julian date).
Another addition to the date object, this one allows you to retrieve the number of a day within the year (sometimes known as the Julian date).
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.
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.
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.
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.
