Jump to content

help with date function


steveclondon

Recommended Posts

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);

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.