Click to See Complete Forum and Search --> : Help shprtening my script....


sciguyryan
11-29-2003, 02:02 AM
does anyone know how to shorten this script I wrote?



<p align="center">
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
var ampm = "a.m."
var mydate = new Date()
var monthday = mydate.getDate()
if (monthday == 4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20||24||25||26||27||28||29||30)
{
var monthday = monthday + "th".sup();
}
else if (monthday == 1||21||31)
{
var monthday = monthday + "st".sup();
}
else if (monthday == 2||22)
{
var monthday = monthday + "nd".sup();
}
else if (monthday == 3||23)
{
var monthday = monthday + "rd".sup();
}
var month = mydate.getMonth()
if (month == 0)
{
var month = "January";
}
else if (month == 1)
{
var month = "Febuary";
}
else if (month == 2)
{
var month = "March";
}
else if (month == 3)
{
var month = "April";
}
else if (month == 4)
{
var month = "May";
}
else if (month == 5)
{
var month = "June";
}
else if (month == 6)
{
var month = "July";
}
else if (month == 7)
{
var month = "August";
}
else if (month == 8)
{
var month = "September";
}
else if (month == 9)
{
var month = "October";
}
else if (month == 10)
{
var month = "November";
}
else if (month == 11)
{
var month = "December";
}
var day = mydate.getDay()
if (day == 1)
{
var day = "Monday";
}
else if (day == 2)
{
var day = "Tuesday";
}
else if (day == 3)
{
var day = "Wednesday";
}
else if (day == 4)
{
var day = "Thursday";
}
else if (day == 5)
{
var day = "Friday";
}
else if (day == 6)
{
var day = "Saturday";
}
else if (day == 0)
{
var day = "Sunday";
}
var year = mydate.getYear()
var hours = mydate.getHours()
if (hours >= 12)
{
hours = hours - 12;
ampm = "p.m."
}
if (hours == 0)
{
hours = 12;
}
var minutes = mydate.getMinutes()
if (minutes <= 9)
{
minutes = "0" + minutes;
}
document.write("The Time is: " + hours + ":" + minutes +" " + ampm)
document.write("<br>" + "The full date is: "+ day +", " + month + " the " + monthday + " " + year + "<br>")
//-->
</SCRIPT>
</P>

Pittimann
11-29-2003, 02:56 AM
Hi!

If you can live with a very small modification in the output, here is some short code:

<script language="JavaScript" type="text/javascript">
<!--
var today = new Date();
var hrs = today.getHours();
ampm = (hrs > 11) ? "p.m." : "a.m.";
timestr=today.toLocaleString();
datestr=today.toLocaleString();
timestr='The time is: '+timestr.substring(timestr.length-8,timestr.length-3)+' '+ampm;
document.write(timestr+'<br>');
datestr='The full date is: '+datestr.substring(0,datestr.length-8)
document.write(datestr)
//-->
</script>

If you could even live with the output of toLocaleString() alone (just adding ampm), you can get the shortest version of displaying the full date and time like this:

<script language="JavaScript" type="text/javascript">
<!--
var today = new Date();
var hrs = today.getHours();
ampm = (hrs > 11) ? "p.m." : "a.m.";
document.write('Date and time: '+today.toLocaleString()+ ' '+ampm);
//-->
</script>

Cheers - Pit

sciguyryan
11-29-2003, 03:16 AM
Thanks, I will imliment it but, I was looking for a FULL SHORTENING....

Pittimann
11-29-2003, 03:20 AM
Hope you're not looking for a code shortened to zero :D

Cheers - Pit

fredmv
11-29-2003, 03:20 AM
I got it down to two lines. Same exact format is intact and I also fixed a bug in your code (it didn't return the correct year in all browsers except IE). ;)

sciguyryan
11-29-2003, 04:20 AM
WOW thanks mate you had exactly what I wanted. I owe you big, Thanks!

fredmv
11-29-2003, 04:24 AM
No problem. ;)

Pittimann
11-29-2003, 10:00 AM
Hi!

Please correct me, if I'm wrong - I'm not a native English speaker (or writer). But as far as I know the ordinal numbers for 11, 12 and 13 are 11th, 12th and 13th.

The code you posted, fredmv, would make them: 11st, 12nd and 13rd. If my assumption is correct, the code should be altered like follows:

<script type="text/javascript">
//<![CDATA[
var n = new Date,d=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'], m = ['January','February','March','April','May','June','July','August','September','October','November',' December'],e=n.getDate().toString().charAt((n.getDate().toString().length>1)?1:0),f=function(n){return (n<10)?'0'+n:n;};
document.writeln('<div style="text-align: center;">The time is: '+((f(n.getHours())>=12)?f(n.getHours())-12:f(n.getHours()))+':'+f(n.getMinutes())+' '+((n.getHours()>=12) ? "PM":"AM")+'<br />The full date is: ' + d[n.getDay()] + ', '+m[n.getMonth()]+' the '+n.getDate()+((n.getDate()==11)?"th".sup():(n.getDate()==12)?"th".sup():(n.getDate()==13)?"th".sup():(e=="1")?"st".sup():(e=="2")?"nd".sup():(e=="3")?"rd".sup():"th".sup())+', '+n.getFullYear()+'</div>');
//]]>
</script>

Cheers - Pit

Pittimann
11-29-2003, 10:10 AM
Sorry - I must have been sleeping!

The originally posted code is some sort of confirmation for my assumption:
if (monthday == 4||5||6||7||8||9||10||11||12||13||14||15||16||17||
18||19||20||24||25||26||27||28||29||30)
{
var monthday = monthday + "th".sup();
}
else if (monthday == 1||21||31)
{
var monthday = monthday + "st".sup();
}
else if (monthday == 2||22)
{
var monthday = monthday + "nd".sup();
}
else if (monthday == 3||23)
{
var monthday = monthday + "rd".sup();
}

Cheers - Pit

fredmv
11-29-2003, 10:35 AM
Oops. Minor typo. Good job finding that bug. I wrote that at 5AM this morning, I should've just went to bed. :p

Pittimann
11-29-2003, 10:45 AM
Hi fredmv!

Now, that you confirmed, that theoretically I was some sort of right, let me add the decicive part a bit shorter (sciguyryan loves short code):

((n.getDate()==11||n.getDate()==12||n.getDate()==13)?"th".sup():(e=="1")?"st".sup():(e=="2")?"nd".sup():(e=="3")?"rd".sup():"th".sup())

Do you no a way to make it even shorter??

Cheers - Pit (hope you slept well and enough in the meantime) :-) - had to switch off smilies...