Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    1350054950348
    1350127555356
    1350645995715
    1352636815355
    Any wisdom would be appreciated!!

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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;

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Fantastic! I know it had to be something simple that I had misunderstood!

    Thanks mrhoo.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •