Site hosted by Angelfire.com: Build your free website today!

DateFormat Home PageRated Top 25% WebApplet by JARS

DateFormat is a set of JavaScript functions that allow you to "feed" DateFormat a format code and a date object (such as new Date() for the current date or document.lastModified for the current pages last modified date). DateFormat then parses the format code with the date object and returns a string representing the date in the format that was specified.

 

Y2K Update!

If you downloaded a version of DateFormat from before January 14, 1999, your version contained a bug and will not work with dates past 1999. You should update it soon with the version found later on this page. This version is now fully Y2K compatible in all tests so far.

 

Test Script

This script allows you to try out some DateFormat codes and get an idea of what it can do.

Input a format code (see below) in the first text box. Then choose whether you want to see the output using the current date (new Date()) or the last modified date of this page (document.lastModified). Press [Make date] to see the date selected in the format specified by the format code you entered.

Please note: This test script below is not DateFormat on its own; a separate script on this page uses DateFormat to run this test. DateFormat can and would normally be used in another JavaScript script for different purposes.
Input a date code:

Date to be shown:

Formatted:

 

Format Codes

The following table shows the codes you can use in a date format code. Any text found in the format code that does not match something in this table will be displayed normally in the output.

Code Meaning
!m the month property of the date object as a 1 or 2-digit number
!mm the month of the year as a 2-digit number
!d the date of the month as a 1 or 2-digit number
!dd the date of the month as a 2-digit number
!yy the two-digit abbreviation for the year
!yyyy the year as a four-digit number
!Day the day of the week as an English word
!Dy the day of the week in its short form, such as "Fri"
!Month the month of the year as an English word
!Mon the month of the year in short form such as "Oct"

Make sure you put the exclamation marks in the proper spot. For example, here are some example outputs using a date some time in January.
Code Output (in January)
!mm 01
!m!m 11
m!m m1
m!mm m01

 

Examples

Here are some example date format codes to try. Click on the link to see the output.  

DateFormat Standard Functions

If you would like to use DateFormat in a JavaScript script of your own, below is the code required. To use it in a program, make a call to dateFormat(format,dateobj) where format is the format code (see above) and dateobj is a Date object, created by new Date() or something similar, and it will return a string representing the date in the format you specified. See this page in Netscape's JavaScript Reference for more information on Date objects.

If you use DateFormat I would appreciate if you would register.

<SCRIPT LANGUAGE="JavaScript">
<!--
/*
 * DateFormat
 * Copyright February 13, 1998 by Jordan Hiller
 * Returns a date string in many possible formats
 * DateFormat may be used for non-commercial purposes
 * as long as this comment remains unmodified.
 * Tested and works in NN4 and IE3
 */
function dateFormat(format,dateobj){

months = new Array(
"January","February","March","April",
"May","June","July","August","September",
"October","November","December");

mons = new Array(
"Jan","Feb","Mar","Apr","May","Jun","Jul",
"Aug", "Sep","Oct","Nov","Dec");

days = new Array(
"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"
);

dys = new Array(
"Sun","Mon","Tues","Wed","Thurs","Fri","Sat");

// Build vars
m = dateobj.getMonth() + 1;
d = dateobj.getDate();
if(dateobj.getYear() > 1900)
  yyyy = dateobj.getYear();
else
  yyyy = 1900 + dateobj.getYear();


Day = days[dateobj.getDay()];
Dy = dys[dateobj.getDay()];
Month = months[m-1];
Mon = mons[m-1];

if(m.toString().length < 2)
  {
  mm = "0" + m + "";
  }else
  {
  mm = m;
  }

if(d.toString().length < 2)
  {
  dd = "0" + d + "";
  }else
  {
  dd = d;
  }

codes = new Array("Month","Mon","mm","m","Day","Dy","dd","d","yyyy","yy");

lastmstring = format;

replaceOld("!Month",Month);
replaceOld("!Mon",Mon);
replaceOld("!mm",mm);
replaceOld("!m,m);
replaceOld("!Day",Day);
replaceOld("!Dy",Dy);
replaceOld("!dd",dd);
replaceOld("!d",d);
replaceOld("!yyyy",yyyy);
replaceOld("!yy",yy);
return lastmstring;
}

/* Note: the function replaceOld() does not need to be accessed by your script
at all, but the function DateFormat() uses it internally. Make sure you copy it,
but otherwise ignore it. */

function replaceOld(before,after){
indexOfIt = lastmstring.indexOf(before);
if(indexOfIt >= 0)
  {
  beforeIt = lastmstring.substring(0, indexOfIt);
  afterIt = lastmstring.substring(indexOfIt + before.length, lastmstring.length);
  lastmstring = beforeIt + after + afterIt;
  replaceOld(before,after);
  return true;
  }
return false;
}
//-->
<SCRIPT>
 

Register to Use It

Your name:
Are you planning to use DateFormat, already using DateFormat, or not planning to use DateFormat in the near future?

By Jordan Hiller. Copyright © February 1998.