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 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts

    going from UTC miliseconds to regular date string?

    i dont know why but this is causing me so much trouble. i just want to add four weeks to a date.

    the first thing i do is convert the date to miliseconds, add 4 weeks worth of miliseconds to it now i want to convert back, but this is where i am stuck.

    Code:
    function start_to_enroll(){
    	var miliDay    	= 86400000;
    	var startField 	= document.getElementById('start_date');
    	var enrollField = document.getElementById('enroll_date');
    	
    	startField.onblur = function(){
    		mili4weeks = 28 * miliDay;
    		miliDate   = utc_date(startField.value);
    		total 	   = miliDate + mili4weeks;
    
    	}
    }
    i just want to put total back to a date string i tried total.getUTCMonth etc. but it returns undefined.

    thanks

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to use the Date object.

    Please show us an example of what startField.value might be set to.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    mm/dd/yyyy

    utc_date takes that kind of string and converts it to milliseconds. that works fine and so does adding the 4weeks of milliseconds. it give me a big integer that id like to convert back to mm/dd/yyyy

  • #4
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <title></title>
    <script type="text/javascript">
    function start_to_enroll(){
    	var miliDay    	= 86400000;
    	var startField 	= document.getElementById('start_date');
    	var enrollField = document.getElementById('enroll_date');
    	
    	startField.onblur = function(){
    		var mili4weeks	= 28 * miliDay;
    		var miliDate	= Date.parse(startField.value);
    		var total	= miliDate + mili4weeks;
    		var newDate	= new Date();
    		newDate.setTime(total);
    		enrollField.value = (newDate.getMonth()+1)+'/'+newDate.getDate()+'/'+
    			newDate.getFullYear();
    	}
    }
    window.onload = start_to_enroll;
    </script>
    </head>
    <body>
    
    <form action="#"><div>
    <input type="text" id="start_date" value="05/23/2006">
    <input type="text" id="enroll_date" value="">
    </div></form>
    
    </body>
    </html>
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Turn your milliseconds back into a date and use the date string method:

    var str=new Date(ms).toLocaleDateString();


    Code:
    	Date.prototype.addDay= function(n,boo){
    		if(!n) n= 1;		
    		var d= this.setDate(this.getDate()+n);
    		d=new Date(d);
    		return (boo)? d.toUTCString():d;
    	};

    Code:
    function incrementDate(d1,n,boo){			
    	var D=new Date(d1)
    	var ND=D.addDay(n,boo);
    	return new Date(ND).toLocaleDateString();
    }
    EG:
    incrementDate('12/25/2006',28,true);// true for UTC time
    Last edited by mrhoo; 05-24-2006 at 05:24 AM.

  • #6
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    thanks works perfectly

    quick question: why does this work
    Date.parse(startField.value);

    but not when i pass it to this function that i made

    utc_date(startField.value);

    function utc_date(date){
    dates = date.split('/');
    return Date.UTC(dates[2], dates[1], dates[0], 0, 0, 0, 0);
    }

    if i use that function insted of Date.parse, i get all types of crazy dates

  • #7
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You have the month and day arguments backwards

    You could change it to
    Code:
    function utc_date(date){
    dates = date.split('/');
    return Date.UTC(dates[2], dates[0], dates[1], 0, 0, 0, 0);
    }
    but then, since Date.parse() is available, I don't see the point in writing your own function to do the same thing.

    Also, if you decide to use the code I posted, you should change
    Code:
    		var newDate	= new Date();
    		newDate.setTime(total);
    to
    Code:
    		var newDate	= new Date(total);
    Today I realized that the Date constructor could take an argument in milliseconds.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #8
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    cool, thanks again for your help

    i did this, as a guess

    var newDate = new Date(total);

    but since my utc_date function was incorrect and i was unaware of Date.parse(), it was feeding me the wrong answer
    Last edited by emehrkay; 05-25-2006 at 02:19 PM.


  •  

    Posting Permissions

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