Page 1 of 1
[SOLVED] Quirky Javascript Date Result
Posted: Mon Dec 19, 2005 10:00 pm
by charp
This is kind of weird because everything was working fine, but at some point things stopped working in Safari.
Here's the code in question:
Code: Select all
var date1 = document.adder.dateStart.value;
var date0 = new Date(date1);
date0.setDate(date0.getDate() - 1);
var month0 = date0.getMonth() + 1;
alert (month0);
var day0 = date0.getDate();
alert (day0);
var year0 = date0.getYear();
alert (year0);
I added the alerts to track what was going on and found that Safari returns 105 for the year, whereas Firefox (and apparently all PC browsers) return 5. I'm 100% certain Safari didn't do this when I first wrote this script about a year ago. Does any one have a clue what's going on here?
Thanks in advance!
Posted: Wed Dec 21, 2005 7:31 pm
by charp
Well I finally solved my own problem. Since no one replied to my post, I'm not sure if my problem stumped everyone or if my question was so lame that you all knew I'd figure it out on my own.
At any rate, here's the link where I found my answer:
http://www.quirksmode.org/js/introdate.html#year
Posted: Mon Jan 09, 2006 8:52 pm
by Afterlife(69)
Use my clock if you like, more features.
Script:
Code: Select all
<script language="Javascript" type="text/javascript">
<!--
function findObj(objId)
{
var element = objId;
if( document.getElementById(objId) )
{
element = document.getElementById(objId);
}
else if( document.all[objId] )
{
element = document.all[objId];
}
else if( document.layers[objId] )
{
element = document.layers[objId];
}
return element;
}
function startClock(element, dformat)
{
var today = new Date();
var month = today.getMonth() + 1;
var day = today.getDate();
var year = today.getFullYear();
var dayNum = today.getDay();
var myDays= ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var theDay = myDays[dayNum];
var seconds = today.getSeconds();
var minutes = today.getMinutes();
var hours = today.getHours();
if(seconds < 10)
{
var seconds = '0' + seconds;
}
if(minutes < 10)
{
var minutes = '0' + minutes;
}
if( hours > 11 )
{
var Suffix=" PM"
}
else
{
var Suffix = " AM"
}
if( hours > 12 )
{
var hours = hours - 12
}
if( hours == 0 )
{
var hours = 12;
}
var fformat = dformat.replace('.d', day);
var fformat = fformat.replace('.M', month)
var fformat = fformat.replace('.Y', year);
var fformat = fformat.replace('.h', hours);
var fformat = fformat.replace('.m', minutes);
var fformat = fformat.replace('.s', seconds);
var fformat = fformat.replace('.S', Suffix);
var fformat = fformat.replace('.D', theDay);
var theTime = fformat;
if( object = findObj(element) )
{
object.innerHTML = theTime;
}
setTimeout("startClock('" + element + "', '" + dformat + "')", 1000);
}
//-->
</script>
Syntax:
Code: Select all
onload="startClock('jsclock', '<b>Time:</b> .D .M/.d/.Y, .h:.m:.s .S')"
Placement: