steveclondon Posted January 13, 2008 Share Posted January 13, 2008 Hi, I have writen a date function that will get a number of days from a form and then add this onto the current date. It does all of this using epoch time. At the moment I am making an alert box that shows the new date. My problem is that I only want this in the format of just the date without the time. I get everything. Here is the code. I have changed the below code so it should just add three days to the current date. var oneMinute = 60 * 1000 ; // milliseconds in a minute var oneHour = oneMinute * 60; var oneDay = oneHour * 24; var oneWeek = oneDay * 7;} //check first that it is a number number=3 var today = new Date(); var dateInMS = today.getTime() + oneDay * number; var targetDate = new Date(dateInMS); targetDate=Date(dateInMS); alert(targetDate); Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 13, 2008 Share Posted January 13, 2008 <script language="javascript"> var oneMinute = 60 * 1000 ; // milliseconds in a minute var oneHour = oneMinute * 60; var oneDay = oneHour * 24; var oneWeek = oneDay * 7; //check first that it is a number number=3 var today = new Date(); var dateInMS = today.getTime() + oneDay * number; var targetDate = new Date(dateInMS); targetDate=Date(dateInMS); var getDate = targetDate.split(" "); alert(""+getDate[0]+" "+getDate[1]+" "+getDate[2]+" "+getDate[3]+""); </script> Quote Link to comment Share on other sites More sharing options...
steveclondon Posted January 13, 2008 Author Share Posted January 13, 2008 Cheers mate, great help Quote Link to comment Share on other sites More sharing options...
steveclondon Posted January 13, 2008 Author Share Posted January 13, 2008 did that, have just seen that it still comes up with todays date. Without the split it will add on three days, with the split it has todays date. Any ideas? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 13, 2008 Share Posted January 13, 2008 did that, have just seen that it still comes up with todays date. Without the split it will add on three days, with the split it has todays date. Any ideas? what exactly do you mean? EDIT I realized that IE has a different variation of the date object; so below is an updated code example, that should display like you would like it to. <script language="javascript"> var oneMinute = 60 * 1000 ; // milliseconds in a minute var oneHour = oneMinute * 60; var oneDay = oneHour * 24; var oneWeek = oneDay * 7; //check first that it is a number number=3 var today = new Date(); var dateInMS = today.getTime() + oneDay * number; var targetDate = new Date(dateInMS); targetDate=Date(dateInMS); var getDate = targetDate.split(" "); var yr = today.getFullYear(); alert(""+getDate[0]+" "+getDate[1]+" "+getDate[2]+" "+yr+""); </script> Quote Link to comment Share on other sites More sharing options...
steveclondon Posted January 15, 2008 Author Share Posted January 15, 2008 im using firefox at the moment, and would want this to work in ie firefox and the other browsers. The bit of code that splits the date down only returns the current date, it is not adding any days on. The code below does two alerts, the first is the full date and time which does show the days added (this is fine, but I don't want the time to be displayed, just the date). The other alert below will display just the date, however it will not show the future date with the extra days added, it will only show todays date. Looking at the code, I can't see why that is? var oneMinute = 60 * 1000 ; // milliseconds in a minute var oneHour = oneMinute * 60; var oneDay = oneHour * 24; var oneWeek = oneDay * 7; //check first that it is a number number=formPlaceAdvert.advertDays.value; var today = new Date(); var dateInMS = today.getTime() + oneDay * number; var targetDate = new Date(dateInMS); alert(targetDate);//this way shows todays date and time + 3 days var oneMinute = 60 * 1000 ; // milliseconds in a minute var oneHour = oneMinute * 60; var oneDay = oneHour * 24; var oneWeek = oneDay * 7; //check first that it is a number var today = new Date(); var dateInMS = today.getTime() + oneDay * number; var targetDate = new Date(dateInMS); targetDate=Date(dateInMS); var getDate = targetDate.split(" "); var yr = today.getFullYear(); alert(""+getDate[0]+" "+getDate[1]+" "+getDate[2]+" "+yr+""); //(this one just shows todays date, not the extra three days that need to be added.) Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 16, 2008 Share Posted January 16, 2008 try this: <script language="javascript"> function currentVal() { var today = new Date(); var ihrn = today.getDate(); document.getElementById("now").value=""+ihrn+""; } function showDate() { var today = new Date(); var later = document.getElementById("advertDays").value; var normal = document.getElementById("now").value; var aiau = (later*1) + (normal*1) + (3*1); // current field value + 3 days var future = today.setDate(aiau); var now=""+today+""; var later = now.split(" "); var month = later[1]; var date = later[2]; var yr = today.getFullYear(); alert(""+later[0]+" "+later[1]+" "+later[2]+" "+yr+""); } window.onload=function() { currentVal(); } </script> <form onsubmit="return false"> <input type="text" id="advertDays"> <input type="hidden" id="now"> <input type="button" value="Show That Date" onclick="showDate()"> </form> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.