Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

Related items

Why bother with JavaScript?

JavaScript Games #2 - Solitaire

Writing a midi hifi system in JavaScript

JavaScript Beginners Start Here

Keeping Count of Downloads

JavaScript Games

Online JavaScript Resources

Automating the Previous and Next buttons

Reading the contents of a directory

Arguments Array

Automating NEW!

You are here: irt.org | Articles | JavaScript | Miscellaneous | Automating NEW! [ previous next ]

Published on: Thursday 3rd April 1997 By: Martin Webb

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() (Coordinated Universal 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 Coordinated Universal 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>

Related items

Why bother with JavaScript?

JavaScript Games #2 - Solitaire

Writing a midi hifi system in JavaScript

JavaScript Beginners Start Here

Keeping Count of Downloads

JavaScript Games

Online JavaScript Resources

Automating the Previous and Next buttons

Reading the contents of a directory

Arguments Array

Feedback on 'Automating NEW!'

©2018 Martin Webb