web blazonry web development scripts and tutorials Get best price on Unlocked Samsung Galaxy A21
   PHP       Name Generators       Perl       CSS       Javascript       Java       MySql       How Tos       Resources   

Javascript Home


image scripts
  Image Load Order
  Mouse Overs
  SlideShow

JavaScript Resources
  Free JavaScript Books(new)
  Recommended Sites
  Bestselling Books

Bookmark and Share




JavaScript: Date Functions

Automatically display the date a web page or file was last modified. This is useful because when you change a file, your web page displays a new date without any changes to HTML. The document object has a last modified date property.

Here's the code:

<SCRIPT LANGUAGE="JavaScript">
<!--
    document.write(document.lastModified);
//-->
</SCRIPT>


This displays:


Improving The Format of the Date Displayed
Usually the date displays in a reasonably nice, user-friendly format, which is determined by locale and browsers. Still, it is easy to modify what is displayed by using JavaScript's Date Object methods and make the date display how you want.

Create a Date Object out of the document.lastModified, and use getDate(), getYear(), getMonth() to build the date so it looks the way you want.

Example:

<SCRIPT LANGUAGE="JavaScript">
<!--

myDate = new Date(document.lastModified);
displayDate = myDate.getMonth()+1 + "-" + myDate.getDate()
document.writeln("Last Modifed: " + displayDate)

//-->
</SCRIPT>

This displays:

More Date Formatting Functions
I have a couple of common date functions I use often to format dates so the name of the month is written out. View the code for these date formatting functions.

Using these functions the above date is displayed as:
which is good for avoiding confusion over which number is the day and which number is the month. This confusion is best avoided because some countries display dates as day/month/year rather than month/day/year.

Two things to remember when using JavaScript dates:

  • the getMonth() method returns values 0-11, not 1-12. (0=January, 1=February, ...)

  • the method getFullYear() was added to JavaScript with 4.0 browsers. It retrieves the 4-digit year instead of just 2 digits. This should be used when possible for Y2K reasons. Now that few people use old, old browsers, this is not much of an issue. But in case it matters to you, just be aware of it.

 

Newest Pages
Test Google Ads Ver. 2
Free Linux Admin Books
Free Linux Books for Programmers
Free Books for Linux on the Desktop
Free PHP Books
Free JavaScript Books
Free Java Books - Advanced
Free Java Books - Basic
Free Perl Books
Free Python Books
Quote of the Day (PHP)
Debugging Part 2
How to Test Google Ads
Most Popular Pages
Baby Name Generator
U.S. Name Generator
Wu Name Generator
Popup Windows (JavaScript)
Upload and Resize an Image (PHP)
How To Install Apache + PHP + MySQL
Intro to Web Databases (PHP, MySQL)

Least Popular Pages
iNews Applet (Java)
Java Resources
Site Monitor (Perl)
PHP Resources
 
 

  privacy policy     ||     © 1997-2016. astonishinc.com   All Rights Reserved.