[SOLVED] Quirky Javascript Date Result

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

[SOLVED] Quirky Javascript Date Result

Post 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!
Last edited by charp on Wed Dec 21, 2005 7:31 pm, edited 1 time in total.
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post 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. :wink:

At any rate, here's the link where I found my answer:

http://www.quirksmode.org/js/introdate.html#year
Afterlife(69)
Spammer :|
Posts: 14
Joined: Mon Oct 03, 2005 4:51 am

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

Code: Select all

<span id="jsclock"></span>
Post Reply