newjsguy Posted March 20, 2009 Share Posted March 20, 2009 OK, first off I am not very good with javascript. I am working on a website that requires some of the pages have the last date modified posted on the page. I know there are websites that use a simple javascript code to post this using document.lastModified. I can visit these other sites using Chrome or Safari and the date appears just fine. However, when I upload a page with a similar code to my server, Chrome and Safari return "Invalid date" or "NaN". I have even ripped the code from one of these other sites and uploaded and still it doesn't work. Here's the link with the code below it. The file contains two scripts, both are supposed to do essentially the same thing except one is much shorter. I'm not worried about the formatting I just want to know why the date is not coming through for me on Safari and Chrome. http://www.lib.iastate.edu/arch/rgrp/test.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title> JavaScript Example 1-2 -- Displaying File Modification Date and URL </title> <meta http-equiv="Content-Script-Type" content= "text/javascript"> </head> <body> <h1> JavaScript Example 1-2<br> Displaying File Modification Date and URL </h1> <script type="text/javascript" language="JavaScript1.1"> <!-- function dayofWeek(day) { switch( day ) { case 0: s = "Sunday"; break; case 1: s = "Monday"; break; case 2: s = "Tuesday"; break; case 3: s = "Wednesday"; break; case 4: s = "Thursday"; break; case 5: s = "Friday"; break; case 6: s = "Saturday"; break; default: s = "Unknownday" } return s; } function monthofYear(mon) { switch( mon ) { case 0: s = "January"; break; case 1: s = "February"; break; case 2: s = "March"; break; case 3: s = "April"; break; case 4: s = "May"; break; case 5: s = "June"; break; case 6: s = "July"; break; case 7: s = "August"; break; case 8: s = "September"; break; case 9: s = "October"; break; case 10: s = "November"; break; case 11: s = "December"; break; default: s = "Unknownmonth" } return s; } testDate = new Date(document.lastModified) testDay = dayofWeek(testDate.getDay()) lastmod = document.lastModified // get string of last modified date lastmoddate = Date.parse(lastmod) // convert modified string to date if(lastmoddate == 0){ // unknown date (or January 1, 1970 GMT) document.writeln("Last Modified: Unknown") }else if(testDay == "Unknownday") { document.write(testDate); }else { d = new Date(lastmod); document.write("Day=", day=dayofWeek(d.getDay()), ", "); document.write("Month=", mon=monthofYear(d.getMonth()), ", "); document.write("Date=", dte=d.getDate(), ", "); document.write("Year=", year=d.getYear(), "<br>"); document.write("Last Modified: " + day + ", " + mon + " " + dte + ", " + year); }// --> </script> <script type="text/javascript" language="JavaScript1.1"> <!-- lastmod = document.lastModified // get string of last modified date lastmoddate = Date.parse(lastmod) // convert modified string to date if(lastmoddate == 0){ // unknown date (or January 1, 1970 GMT) document.writeln("Last Modified: Unknown") } else { document.writeln("Last Modified: " + lastmod) }// --> </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
paulalbert Posted April 1, 2009 Share Posted April 1, 2009 <html> <head> <title>Last Modified</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> function showLastModified() { var out = document.getElementById('lastModified'); var d = new Date(); if (d.toLocaleDateString) { out.innerHTML = d.toLocaleDateString(document.lastModified); } else { out.innerHTML = document.lastModified; } } window.onload = showLastModified; </script> </head> <body> Last Modified on <span id="lastModified"> </span> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.