Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Firefox Javascript Date object issue

  • 29-03-2006 04:22PM
    #1
    Closed Accounts Posts: 169 ✭✭


    I'm having a bit of trouble with a date copying function I've tried implimenting. What it does is takes a string in the format "HH:MM" to represent the time and returns a date object with the same date but a specific time.

    The code responsible for this is
    Date.prototype.copy=function(time){
    	copy = new Date();
    	copy.setDate(this.getDate());
    	copy.setMonth(this.getMonth());
    	copy.setYear(this.getYear());
    	bits=time.split(/:/);
    	copy.setHours(bits[0]);
    	copy.setMinutes(bits[1]);
    	// Debugging info
    	document.getElementById("testDiv").innerHTML+="Turning "+bits[0]+" section of time string "+time+" into an hour value of "+copy.getHours()+"<br/>Created date object with value "+copy+"<br/><br/>";
    	return copy;
    }
    

    But it's producing the following date values
    Turning 00 section of time string 00:00 into an hour value of 0
    Created date object with value Sun Mar 28 0106 00:00:44 GMT+0000 (GMT Standard Time)
    
    Turning 00 section of time string 00:59 into an hour value of 0
    Created date object with value Sun Mar 28 0106 00:59:44 GMT+0000 (GMT Standard Time)
    
    Turning 01 section of time string 01:00 into an hour value of 0
    Created date object with value Sun Mar 28 0106 00:00:44 GMT+0000 (GMT Standard Time)
    
    Turning 01 section of time string 01:59 into an hour value of 0
    Created date object with value Sun Mar 28 0106 00:59:44 GMT+0000 (GMT Standard Time)
    
    Turning 02 section of time string 02:00 into an hour value of 2
    Created date object with value Sun Mar 28 0106 02:00:44 GMT+0100 (GMT Daylight Time)
    
    Turning 02 section of time string 02:59 into an hour value of 2
    Created date object with value Sun Mar 28 0106 02:59:44 GMT+0100 (GMT Daylight Time)
    
    Turning 03 section of time string 03:00 into an hour value of 3
    Created date object with value Sun Mar 28 0106 03:00:44 GMT+0100 (GMT Daylight Time)
    
    Turning 03 section of time string 03:59 into an hour value of 3
    Created date object with value Sun Mar 28 0106 03:59:44 GMT+0100 (GMT Daylight Time)
    

    It's only the value for 01:00 that's off in all 24 hours of the day, and it changes the GMT.

    The code runs perfectly in internet explorer.

    Any help on this front would be appreciated.


Comments

  • Closed Accounts Posts: 169 ✭✭akari no ryu


    This situation has been resolved. I'd love to know what I did wrong, if anyone can tell me, but I'll post the workaround:
    Date.prototype.copy=function(time){
    	copy = new Date();
    	copy.setUTCDate(this.getDate());
    	copy.setUTCMonth(this.getMonth());
    	copy.setUTCFullYear(this.getFullYear());
    	bits=time.split(/:/);
    	copy.setUTCHours(bits[0]);
    	copy.setUTCMinutes(bits[1]);
    	return copy;
    }
    


Advertisement