Forum Moderators: open

Message Too Old, No Replies

Previous Day Display Script

         

jmarch51

4:11 pm on Jul 28, 2006 (gmt 0)

10+ Year Member



Hi

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

jmarch51

4:27 pm on Jul 28, 2006 (gmt 0)

10+ Year Member



I just found a solution:

<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?

rocknbil

6:33 pm on Jul 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard jmarch, it's always "TMTOWTDI." :-) Here's a little script to explore the date object, all the tools are there but here's what you have to do. You have to subtract one day from the current day, and if that subtraction produces zero you have to determine the last day of the previous month, if that month is less than zero (January = 0) then you have to use month 11 December) and decrement the year.

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>

Fotiman

7:29 pm on Jul 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, there is a better way. You really should be doing this server side for a couple of reasons.

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]

Fotiman

7:43 pm on Jul 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



And if you want to also show the day of the week...



<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>

Fotiman

7:50 pm on Jul 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



And even better yet still, you can display the date/time based on the users Locale. People in the U.S. would see dates differently than, say, people in France.



<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]

jmarch51

8:11 pm on Jul 28, 2006 (gmt 0)

10+ Year Member



Thanks for the help guys! Probably a dumb question but how would I go about doing it server side?

Fotiman

8:35 pm on Jul 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It depends on what your server supports. If it's Linux OS, you'll probably want to use PHP to do it. If it's a Windows server, then ASP or ASP.NET might be a better option (or PHP may be installed as well).

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?

jmarch51

3:09 pm on Jul 31, 2006 (gmt 0)

10+ Year Member



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?

Fotiman

3:50 pm on Jul 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




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.

jmarch51

7:03 pm on Aug 1, 2006 (gmt 0)

10+ Year Member



thanks for the help