Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-12-2012, 12:28 PM   PM User | #1
mattps
New to the CF scene

 
Join Date: Aug 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
mattps is an unknown quantity at this point
setDate madness!

This is driving me mad and is probably a simple error on my part and I would appreciate a pair of fresh eyes.

I have the following code to increment the date based on a select box selection:

Code:
function calcdeadline()
{
	var myDate=new Date;
	var priority = document.getElementById("priority").options[document.getElementById("priority").selectedIndex].text
	switch (priority)
	{
		case "1": document.getElementById('deadline').value=myDate.setHours(myDate.getHours()+1);break;
		case "2": document.getElementById('deadline').value=myDate.setHours(myDate.getHours()+4);break;
		case "3": document.getElementById('deadline').value=myDate.setDate(myDate.getDate()+1);break;
		case "4": document.getElementById('deadline').value=myDate.setDate(myDate.getDate()+7);break;
		case "5": document.getElementById('deadline').value=myDate.setDate(myDate.getDate()+30);break;
		case "6": break;
	default: break;
	}
return;
}
Everything works as expected except the value that is return by setDate. If I try and increment the current date (12/10/2012) by 4 hours, 1 day, 7 days or 30 days, I get the follow returned "dates":

Quote:
1350054950348
Quote:
1350127555356
Quote:
1350645995715
Quote:
1352636815355
Any wisdom would be appreciated!!
mattps is offline   Reply With Quote
Old 10-12-2012, 01:46 PM   PM User | #2
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
new Date(1350054950348)= Fri Oct 12 11:15:50 EDT 2012


You are using the timestamp that returns from the setHours() method.

set the hours, then read the date string-

Code:
	myDate.setHours(myDate.getHours()+priority);

	document.getElementById('deadline').value=myDate;
mrhoo is offline   Reply With Quote
Old 10-12-2012, 02:28 PM   PM User | #3
mattps
New to the CF scene

 
Join Date: Aug 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
mattps is an unknown quantity at this point
Fantastic! I know it had to be something simple that I had misunderstood!

Thanks mrhoo.
mattps is offline   Reply With Quote
Old 10-12-2012, 05:17 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Perhaps a simpler solution.

Code:
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>

<select id = "priority" onchange = "calcdeadline()">
<option value = 0>Select priority level...</option>
<option value = 1>1 hour</option>
<option value = 4>4 hours</option>
<option value = 24>24 hours</option>
<option value = 168>7 days</option>
<option value = 720>30 days</option>
</select>

<input type = "text" id = "deadline" size = 60 readonly>

<script type = "text/javascript">

function calcdeadline()  {
var myDate=new Date();
var val = Number(document.getElementById("priority").value);  // from dropdown
myDate.setHours(myDate.getHours()+val);
document.getElementById('deadline').value=myDate;
}

</script>

</body>
</html>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:17 AM.


Advertisement
Log in to turn off these ads.