Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deduct number of days from entered date 2

Status
Not open for further replies.

kiwieur

Technical User
Joined
Apr 25, 2006
Messages
200
Location
GB
Hi,

i am trying to deduct 35 days from a date entered by the user. I have 2 text fields named

txtDM & txtKickIn

what I want is that when the user enters a date in txtDM then onChange txtKickin will display that date minus 35

This is the code I am trying to use at the moment but i get "NaN" in txtKickIn

Code:
<script>
function KickIn(){
var txtDM=document.getElementById("txtDM")
var txtKickIn=document.getElementById("txtKickIn")
var NoDays = 35
txtKickIn.value = txtDM - NoDays;
}
</script>

and then this for the onChange

Code:
onChange="KickIn();"

any help would be appreciated

Regards

Paul

 
Hi Paul...
Code:
var txtDM=document.getElementById("txtDM")
What you're grabbing with that line there is a reference to the textbox object itself.
Code:
var txtDM=document.getElementById("txtDM")[COLOR=blue].value;[/color]
Will get you the value out of the textbox. But that value will be a string, so subtracting 35 from it will still give you NaN (Not A Number).

Assuming your users have typed a date into the field, you can instantiate a new Date object to manipulate:
Code:
var dtDM = new Date(txtDM);
Then you can subtract your number of days... however bear in mind that JS Dates are counted in milliseconds rather than days so you really want:
Code:
var NoDays = 35
to be:
Code:
var NoDays = 35 * 24 * 60 * 60 * 1000;
35 days * 24 hours * 60 minutes * 60 seconds * 1000 milli seconds = 35 days expressed as milliseconds.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Hi Dwarfthrower,

Thanks for your reply, i chnaged my code to the following

Code:
<script>
function KickIn(){
var txtDM=document.getElementById("txtDM").value;
var txtKickIn=document.getElementById("txtKickIn")
var dtDM = new Date(txtDM);
var NoDays = 35 * 24 * 60 * 60 * 1000;
txtKickIn.value = dtDM - NoDays;
}
</script>

but now if i enter a date for example 30/10/2007
then i get this value in the txtKickIn field

1241564400000

do you have any ideas why ?


Regards

Paul

 
Yep, shoulda mentioned that... the integer subtraction resolves an integer... cast it back to a date prior to inserting it into your field:
Code:
txtKickIn.value = new Date(dtDM - NoDays);

If you want it to return in a particular format - like the one you used - you will need to use the various .getMonth() .getYear() .getDate() operators of the date object to build a string:
Code:
var dtKickIn = new Date(dtDM - NoDays);
txtKickIn.value = dtKickIn.getDate() + "/" + dtKickIn.getMonth() + "/" + dtKickIn.getYear();

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Hi Dwarfthrower,

I would to like thank you for your help resolving my problem,a "star" is on it's way to you, could you put me on the right direction to also display the kick in date as a weekdayname.

i.e my example date of 30/10/2007 would display as "Tuesday".

Once again thanks for your help

Regards

Paul

 
I can indeed mate, can indeed.

The Date object has a member function [tt]getDay()[/tt] which returns an integer between 0 and 6 inclusive to represent the day of the week (as opposed to [tt]getDate()[/tt] which returns an integer between 1 and 31 representing the day of the month)

Couple that with an Array of Day Names and we're set:

Code:
var arrayOfDayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var dayOfTheWeek = arrayOfDayNames[dtKickIn.getDay()];

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Hi Dwarfthrower,

So I took my time replying but I have been onsite on a job and had no internet connection

Thanks again for some more great info and code, wish I could give you another "star"

Regards

Paul

 
Hi,
I have this code based on some help from dwarfthrower, at the time I tested it very quickly and it appeared to work OK.

Code:
<script>
function KickInD(){
var txtDM=document.getElementById("txtDM2").value;
var txtKickIn=document.getElementById("txtKID2")
var dtDM = new Date(txtDM);
var NoDays = 35 * 24 * 60 * 60 * 1000;
var dtKickIn = new Date(dtDM - NoDays);
txtKickIn.value = dtKickIn.getDate() + "/" + dtKickIn.getMonth() + "/" + dtKickIn.getYear();
}
</script>

However now when I am testing it I am getting these very strange results
if I enter the date 30/10/2007
it returns in the other field 6/4/109

does anyone know where i am going wrong please

Regards

Paul

 
Ok,

I now have so code working which is built from snippets found from various threads, the code is as follows

Code:
<script language="javascript">
function KickIn()
{    var oFrm = document.frmTask;
    var E = getUKDate( oFrm["txtDM"].value ); // convert to date
    var P = 35;

    E.setDate( E.getDate() - P ); // subtract P days
    oFrm["txtKickInDate"].value = date_format( E ); // format output to dd/mm/yyyy
}

function date_format( d )
{    return d.getDate() + "/" +(d.getMonth()+1 ) + "/" + d.getFullYear();
}

function getUKDate( sDate )
{    aParts = sDate.split("/");
    return new Date( aParts[2], aParts[1]-1, aParts[0] );
}
</script>

therfore if i input 30/09/2007
and deduct the value 35
the returned value is 26/8/07

All I need to do now is pad out the day and month if it is only one digit,

could somebody help here please

Regards

Paul

 
One way is this.
>return d.getDate() + "/" +(d.getMonth()+1 ) + "/" + d.getFullYear();
[tt]return ((d.getDate().toString().length==2)?"":"0") + d.getDate() + "/" + (((d.getMonth()+1).toString().length==2)?"":"0") + (d.getMonth()+1) + "/" + d.getFullYear();[/tt]
 
tsuji,

worked like a charm, thank you so much

"Star" on it's way

Regards

Paul

 
hi tsuji,

any idea how I would incorporate the dayname function code dwarfthrower supplied into my new code

dayname code
Code:
var arrayOfDayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var dayOfTheWeek = arrayOfDayNames[dtKickIn.getDay()];

new code
Code:
<script language="javascript">
function KickIn()
{    var oFrm = document.frmTask;
    var E = getUKDate( oFrm["txtDM"].value ); // convert to date
    var P = 35;
	
    E.setDate( E.getDate() - P ); // subtract P days
    oFrm["txtKickInDate"].value = date_format( E ); // format output to dd/mm/yyyy
}

function date_format( d )
{    return ((d.getDate().toString().length==2)?"":"0") + d.getDate() + "/" + (((d.getMonth()+1).toString().length==2)?"":"0") + (d.getMonth()+1) + "/" + d.getFullYear();

}

function getUKDate( sDate )
{    aParts = sDate.split("/");
    return new Date( aParts[2], aParts[1]-1, aParts[0] );
}
</script>

Regards

Paul

 
Well it looks like I got it working with the following I created another function to handle the weekday part
Code:
<script language="javascript">
function KickIn()
{    var oFrm = document.frmTask;
    var E = getUKDate( oFrm["txtDM"].value ); // convert to date
    var P = 35;
    E.setDate( E.getDate() - P ); // subtract P days
    oFrm["txtKickInDate"].value = date_format( E ); // format output to dd/mm/yyyy
}

function date_format( d )
{    return ((d.getDate().toString().length==2)?"":"0") + d.getDate() + "/" + (((d.getMonth()+1).toString().length==2)?"":"0") + (d.getMonth()+1) + "/" + d.getFullYear();

}

function getUKDate( sDate )
{    aParts = sDate.split("/");
    return new Date( aParts[2], aParts[1]-1, aParts[0] );
}

[b][COLOR=blue]function KickIn2()
{    var oFrm = document.frmTask;
    var E = getUKDate( oFrm["txtKickInDate"].value ); // convert to date
	var weekday = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
    oFrm["txtKickInDay"].value = weekday[ E.getDay()]; // format output to dd/mm/yyyy
}
function getUKDate( sDate )
{    aParts = sDate.split("/");
    return new Date( aParts[2], aParts[1]-1, aParts[0] );
}

</script>[/color][/b]

I would really appreciate it if a "Guru" would cast their eyes over it and tell me if I am on the right track

Regards

Paul

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top