Articles | Automating NEW! |  back 

I got the idea for this facility from A Beginner's Guide to JavaScript

Rather than amend which items are new on an infrequent basis, why not automate it with a JavaScript function called check_if_new():

<script language="JavaScript"><!--
function y2k(number) { return (number < 1000) ? number +
 1900 : number; }

function check_if_new(then) {
     var today = new Date();
     var difference = Date.UTC(y2k(today.getYear()),
     today.getMonth(),today.getDate(),0,0,0)
         - Date.UTC(y2k(then.getYear()),then.getMonth(),
     then.getDate(),0,0,0);
     var days_difference = difference/1000/60/60/24;
     if (days_difference < 14)
         return '<em>NEW!<\/em>';
     else return '';
}

date = new Date(1997,2,20);
document.write(check_if_new(date));
//--></script>

Date.UTC() (Universal Coordinated Time) returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00.

For example Date.UTC(1997,0,1), i.e. 1st January 1997, will always return 852076800000.

By subtracting the Universal Coordinated Time of one date from another we can find the difference in milliseconds between two dates.

By dividing this difference by 1000 milliseconds and by 60 seconds and by 60 minutes and then by 24 hours, we can find the difference in days between two dates.

By comparing this difference in days with a static number we can tell whether the date falls within a predetermined range, e.g. 14 days, and if so output the text NEW!.

Alternatively, an image can be output:

<script language="JavaScript"><!--
function y2k(number) { return (number < 1000) ? number +
 1900 : number; }

function check_if_new(then) {
     var today = new Date();
     var difference = Date.UTC(y2k(today.getYear()),
     today.getMonth(),today.getDate(),0,0,0)
         - Date.UTC(y2k(then.getYear()),then.getMonth(),
     then.getDate(),0,0,0);
     var days_difference = difference/1000/60/60/24;
     if (days_difference < 14)
         return '<img src="new.gif">';
     else return '';
}

date = new Date(1997,2,20);
document.write(check_if_new(date));
//--></script>

Also read the article Chocolate Chip Cookies + Automating NEW!

Articles | Automating NEW! |  back

 

Menu

Home
Articles
FAQ's
Links
Tools
Blog Links
AJAXCoded Blog
CyberCoded.net
CyberCoder
CybersView

Applications

 

Questions, Comments
or Suggestions

Site Search
FAQ Search

AJAX Coded
CAAD @ CyberCoded.net


 


Copyright


Google
WWW CyberCoded.com
Provide Feedback on CyberCoded.com

Copyright © 1996-2008 CyberCoded.com, All Rights Reserved.
CyberCoded.com liability, trademark, document use and software licensing rules apply. Your interactions with this site are in accordance with our public privacy statements.
Website Hosted By: USNX

We recommend USNX for quality ASP Web Hosting