[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

last modified with days since last change

Posted on 2004-11-29
5
Medium Priority
?
206 Views
Last Modified: 2011-10-03
i am looking for a javascript that displays when the page was last modified (day name, month, date format) as well as the number of days since it was last changed (2 days ago). so it would read something like:

last modified on mon, nov 29, 0 days ago.

i have found this one, it seems fine, but when i put it on my server it said -1 day when it should have said 0 days. Can someone please help? is there an easy fix, or another script? i want it to just reference the date that i modify it - not anything on the server.

<script language=JavaScript>
<!-- Hide
// Array of day names
var dayNames = new

Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
// Array of month names
var monthNames = new

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

today = new Date()
lastmod = new Date(document.lastModified)
diffdays = Math.floor( (today.getTime()-
lastmod.getTime())/(24*60*60*1000) )
var then = new Date(document.lastModified);
document.writeln("Showroom last updated on "+
dayNames[then.getDay()] + ", " + monthNames[then.getMonth()] + " " + then.getDate() + " | " + diffdays + " days ago")
// Unhide -->
</script>
0
Comment
Question by:chuckp27
  • 4
5 Comments
 
LVL 25

Expert Comment

by:archrajan
ID: 12701861

function writeDateModified (time) {

  var days = new Array;                        // Array to hold day names
  var months = new Array;                      // Array to hold up month names

  // Load up day names
  days[0] = "Sunday";
  days[1] = "Monday";
  days[2] = "Tuesday";
  days[3] = "Wednesday";
  days[4] = "Thursday";
  days[5] = "Friday";
  days[6] = "Saturday";

  // Load up month names
  months[0] = "January";
  months[1] = "February";
  months[2] = "March";
  months[3] = "April";
  months[4] = "May";
  months[5] = "June";
  months[6] = "July";
  months[7] = "August";
  months[8] = "September";
  months[9] = "October";
  months[10] = "November";
  months[11] = "December";

  // Assign date variables with document.lastModified
  var modDate = new Date(Date.parse(document.lastModified));
 
  // If we have a valid date reformat it.
  if (modDate != 0) {
 
    // Set up day variable to hold the name of the day
    var day = days[modDate.getDay()];
   
    // ndate variable holds day of month
    var ndate = modDate.getDate();
   
    // Set up month variable to hold the name of the month
    var month = months[modDate.getMonth()];
   
    // Get the year and if it is less than 1000 add 1900 to it.
    var year = modDate.getYear();
    if (year < 1000) year = year + 1900;
   
    // Load up the time variables if required
    if (time) {
      var hour = modDate.getHours().toString();
      if (hour.length == 1) hour = "0" + hour;
      var minute = modDate.getMinutes().toString();
      if (minute.length == 1) minute = "0" + minute;
      var second = modDate.getSeconds().toString();
      if (second.length == 1) second = "0" + second;
    }
   
    // Display date and time document was last updated.
    document.write(day + " " + ndate + " " + month + " " + year+ "  ");
    if (time) {
      document.write(hour + ":" + minute + ":" + second);
    }
  }
}
0
 
LVL 25

Expert Comment

by:archrajan
ID: 12701924
date difference
function dateDiff(dateform) {
date1 = new Date();
date2 = new Date();
diff  = new Date();

if (isValidDate(dateform.firstdate.value) && isValidTime(dateform.firsttime.value)) { // Validates first date
date1temp = new Date(dateform.firstdate.value + " " + dateform.firsttime.value);
date1.setTime(date1temp.getTime());
}
else return false; // otherwise exits

if (isValidDate(dateform.seconddate.value) && isValidTime(dateform.secondtime.value)) { // Validates second date
date2temp = new Date(dateform.seconddate.value + " " + dateform.secondtime.value);
date2.setTime(date2temp.getTime());
}
else return false; // otherwise exits

// sets difference date to difference of first date and second date

diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

timediff = diff.getTime();

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

days = Math.floor(timediff / (1000 * 60 * 60 * 24));
timediff -= days * (1000 * 60 * 60 * 24);

hours = Math.floor(timediff / (1000 * 60 * 60));
timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60));
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000);
timediff -= secs * 1000;

dateform.difference.value = weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds";

return false; // form should never submit, returns false
}
0
 
LVL 25

Expert Comment

by:archrajan
ID: 12701931
This gives all in one script...
function lastMod()
{
      var x = new Date (document.lastModified);
      Modif = new Date(x.toGMTString());
      Year = takeYear(Modif);
      Month = Modif.getMonth();
      Day = Modif.getDate();
      Mod = (Date.UTC(Year,Month,Day,0,0,0))/86400000;
      x = new Date();
      today = new Date(x.toGMTString());
      Year2 = takeYear(today);
      Month2 = today.getMonth();
      Day2 = today.getDate();
      now = (Date.UTC(Year2,Month2,Day2,0,0,0))/86400000;
      daysago = now - Mod;
      if (daysago < 0) return '';
      unit = 'days';
      if (daysago > 730)
      {
            daysago = Math.floor(daysago/365);
            unit = 'years';
      }
      else if (daysago > 60)
      {
            daysago = Math.floor(daysago/30);
            unit = 'months';
      }
      else if (daysago > 14)
      {
            daysago = Math.floor(daysago/7);
            unit = 'weeks'
      }
      var towrite = 'Page last changed ';
      if (daysago == 0) towrite += 'today';
      else if (daysago == 1) towrite += 'yesterday';
      else towrite += daysago + ' ' + unit + ' ago';
      return towrite;
}


function takeYear(theDate)
{
      x = theDate.getYear();
      var y = x % 100;
      y += (y < 38) ? 2000 : 1900;
      return y;
}

and finally at the place where it should be written

document.write(lastMod())

0
 
LVL 25

Accepted Solution

by:
archrajan earned 1000 total points
ID: 12701944
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<script>
function lastMod()
{
      var x = new Date (document.lastModified);
      Modif = new Date(x.toGMTString());
      Year = takeYear(Modif);
      Month = Modif.getMonth();
      Day = Modif.getDate();
      Mod = (Date.UTC(Year,Month,Day,0,0,0))/86400000;
      x = new Date();
      today = new Date(x.toGMTString());
      Year2 = takeYear(today);
      Month2 = today.getMonth();
      Day2 = today.getDate();
      now = (Date.UTC(Year2,Month2,Day2,0,0,0))/86400000;
      daysago = now - Mod;
      if (daysago < 0) return '';
      unit = 'days';
      if (daysago > 730)
      {
            daysago = Math.floor(daysago/365);
            unit = 'years';
      }
      else if (daysago > 60)
      {
            daysago = Math.floor(daysago/30);
            unit = 'months';
      }
      else if (daysago > 14)
      {
            daysago = Math.floor(daysago/7);
            unit = 'weeks'
      }
      var towrite = 'Page last changed ';
      if (daysago == 0) towrite += 'today';
      else if (daysago == 1) towrite += 'yesterday';
      else towrite += daysago + ' ' + unit + ' ago';
      return towrite;
}


function takeYear(theDate)
{
      x = theDate.getYear();
      var y = x % 100;
      y += (y < 38) ? 2000 : 1900;
      return y;
}



document.write(lastMod())
</script>

</BODY>
</HTML>
0
 

Author Comment

by:chuckp27
ID: 12720975
thanks. even better than i hoped!!!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question