Forum Moderators: open
Is there a script out there that will display the previous date.
I tried looking on Dynamic Drive but couldn't find what I was looking for.
On a page that I am doing the message says "All results are as of yesterday"
Instead of saying yesterday I would like it to have the previous days date listed ie. "All results are as of July 27 20006"
Thanks in advance
Jeff
<script language="javascript">
<!--
Today = new Date();
TodayDay = Today.getDate();
TodayMon = Today.getMonth();
TodayYear = Today.getYear();
TodayDay = TodayDay - 1;
if (TodayYear < 2000) TodayYear += 1900;
if (TodayMon == 0) { TodayMonth = "January"; }
else if (TodayMon == 1) { TodayMonth = "February"; }
else if (TodayMon == 2) { TodayMonth = "March"; }
else if (TodayMon == 3) { TodayMonth = "April"; }
else if (TodayMon == 4) { TodayMonth = "May"; }
else if (TodayMon == 5) { TodayMonth = "June"; }
else if (TodayMon == 6) { TodayMonth = "July"; }
else if (TodayMon == 7) { TodayMonth = "August"; }
else if (TodayMon == 8) { TodayMonth = "September"; }
else if (TodayMon == 9) { TodayMonth = "October"; }
else if (TodayMon == 10) { TodayMonth = "November"; }
else if (TodayMon == 11) { TodayMonth = "December"; }
else { TodayMonth = TodayMon; }
document.write(TodayMonth + " " + TodayDay + ", " + TodayYear);
-->
</script>
Is there a better way to do this?
In mysql there is an easier way using date math, but to my knowledge don't know if there's such an implementation in Javascript . . . . .
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Play with Date and Time</title>
</head>
<body>
<H2>Current Date and Time</H2>
<script type="text/javascript">
<!--
var msg;
var today = new Date();
var Weekday = new Array ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var Months = new Array ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September','October', 'November', 'December');
var dayofweek = today.getDay();
var day = today.getDate();
var mon = today.getMonth();
var year = today.getYear();
if (year < 2000) {year += 1900; }
var CurrHr = (today.getHours() <= 12)?today.getHours():(today.getHours() - 12);
var post = (today.getHours() <= 12)?'AM':'PM';
msg = 'It is currently ' + CurrHr + ':' + today.getMinutes() + ' ' +
post.fontsize(-1) + ' on ' + Weekday[dayofweek] + ', ' + Months[mon] + ' ' + day + ', ' + year + '.<br>';
// More date stuff
//msg += 'Hour only: ' + today.getHours() + '.<br>';
//msg += Minutes only: ' + today.getMinutes() + '.<br>';
//msg += Seconds only: ' + today.getSeconds() + '.<br>';
//msg += Month\(Jan is 0\): ' + today.getMonth() + '.<br>';
//msg += Weekday only(Sunday is 0\): ' + today.getDay() + '.<br>';
//msg += Day only: ' + today.getDate() + '.<br>');
document.write(msg);
//-->
</script>
</body>
</html>
1. If the user has JavaScript disabled.
2. If the user's PC has an incorrect date setting. For example, the user sets the current date on his PC to be July 28, 2906. The website would display the wrong information in that case.
However, if you still want to use JavaScript (note, I removed the language attribute and replaced it the correct attribute "type"):
<script language="javascript">
<!--
var months = new Array( "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" );
var Today = new Date();
// 1 day = 1 * 24 hours = 24 hours
// 24 hours = 24 * 60 min = 1440 min
// 1440 min = 1440 * 60 sec = 86400 sec
// 86400 sec = 86400 * 1000 ms = 86400000 ms
// Subtract 1 day, aka 86400000ms, from Today's date
var Yesterday = new Date( Today.getTime() - 86400000 );
YesterdayMon = months[ Yesterday.getMonth() ];
YesterdayYear = Yesterday.getFullYear();
YesterdayDate = Yesterday.getDate();
document.write(YesterdayMon + " " + YesterdayDate + ", " + YesterdayYear);
-->
</script>
Note, there's no need to know if it's the last day or the week or last day of the month. It's all done automatically by subtracting 1 day from today's date and creating a new date with that value.
[edited by: Fotiman at 7:31 pm (utc) on July 28, 2006]
<script language="javascript">
<!--
var months = new Array( "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" );
var days = new Array( "Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday" );
var Today = new Date();
// 1 day = 1 * 24 hours = 24 hours
// 24 hours = 24 * 60 min = 1440 min
// 1440 min = 1440 * 60 sec = 86400 sec
// 86400 sec = 86400 * 1000 ms = 86400000 ms
// Subtract 1 day, aka 86400000ms, from Today's date
var Yesterday = new Date( Today.getTime() - 86400000 );
YesterdayMon = months[ Yesterday.getMonth() ];
YesterdayYear = Yesterday.getFullYear();
YesterdayDate = Yesterday.getDate();
YesterdayDay = days[ Yesterday.getDay() ];
document.write(YesterdayDay + " " + YesterdayMon + " " + YesterdayDate + ", " + YesterdayYear);
-->
</script>
<script type="text/javascript">
var Today = new Date();
// 1 day = 1 * 24 hours = 24 hours
// 24 hours = 24 * 60 min = 1440 min
// 1440 min = 1440 * 60 sec = 86400 sec
// 86400 sec = 86400 * 1000 ms = 86400000 ms
var Yesterday = new Date( Today.getTime() - 86400000 );
document.write(Yesterday.toLocaleString());
</script>
In the U.S., that would print something like this:
Thursday, July 27, 2006 3:51:49 PM
[edited by: Fotiman at 7:53 pm (utc) on July 28, 2006]
For PHP files, your page needs a .php extension. For ASP, .asp. For ASP.NET, .aspx.
Do you know any of these languages? Do you know what your server supports?
I know a little, so when you say server side do you mean this<!-- #include virtual ="/includes/date.asp" -->
So then I would put the javascript for the date inside date.asp?
I was thinking something more along the lines of this:
Make sure that the file that displays the date has a .asp extension and then in that file...
Yesterday's date:
<%
Dim yesterday
' Subtract 1 "D"ay from the current date
yesterday = DateAdd( "D", -1, Date() )
' Display it using FormatDateTime with the VBLongDate format (1)
Response.Write FormatDateTime( yesterday, 1 )
%>
Granted, this could be split out into a separate file and it could be made to be more object oriented or it could be made to separate out business logic from presentation logic. But I suspect all of that is beyond the scope of the simple task you're trying to do here, which is just display yesterday's date. The above example should work, though I haven't tested it.