emehrkay
05-23-2006, 08:46 PM
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.
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
Kravvitz
05-24-2006, 12:06 AM
You need to use the Date object.
Please show us an example of what startField.value might be set to.
emehrkay
05-24-2006, 01:51 AM
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
Kravvitz
05-24-2006, 05:16 AM
Try this.
<!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>
mrhoo
05-24-2006, 05:17 AM
Turn your milliseconds back into a date and use the date string method:
var str=new Date(ms).toLocaleDateString();
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;
};
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
emehrkay
05-24-2006, 02:25 PM
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
Kravvitz
05-24-2006, 11:30 PM
You have the month and day arguments backwards
You could change it to
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
var newDate = new Date();
newDate.setTime(total);
to
var newDate = new Date(total);
Today I realized that the Date constructor could take an argument in milliseconds.
emehrkay
05-25-2006, 02:15 PM
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