Navigate
Home
ArticleWiki
Forum
Newsletter
Links
Tech News
Welcome Guest.
Username:

Password:

Remember me

Text to date object and Vice versa
Welcome, Guest. Please login or register.
February 08, 2012, 10:36:00 AM
11513 Posts in 1262 Topics by 496 Members
Latest Member: Beerdernill
Experts Round Table Network  |  Clientside Technology  |  Javascript  |  Text to date object and Vice versa « previous next »
Pages: [1] 2
Author Topic: Text to date object and Vice versa  (Read 2449 times)
fastrobby

Offline Offline

Posts: 19


« on: July 09, 2007, 05:07:13 PM »

Hi,
How do I take a text date from a text box and adddnights.value  and return that as a text date to another text box
Nothing that I have done so far works. Closest I have come is to add days (nights) but the months and days are reversed. I am not parsing the text properly to the add days routine. Also the year comes out alright in IE but not in FF

if(thisform.dateIn.value!=' ') {
   thisform.dateOut.value = addDays(document.getElementById('dateIn').value,parseInt(thisform.nightsNo.value));
 
See http://www.kohchangbookingandinformation.com/resorts_2.asp
Dont mind the alerts I have been testing stuff
Thanks for your time and patience
« Last Edit: July 09, 2007, 05:14:03 PM by fastrobby » Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« Reply #1 on: July 10, 2007, 08:43:52 PM »

Wow!  Your addDate is one big function.

Take the string and convert it to a JavaScript Date object, which is the number of milliseconds since January 1, 1970.
Take the number of days and multiply by the number of milliseconds in one day (86400000)
Add the two, and
Convert back to a string.

No need to try to do all the date arithmetic yourself.

http://www.rodsdot.com/ee/add-days-to-javascript-date.asp

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="© 2005-2007 Roderick Divilbiss">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<title>Add Days To Date</title>
<script type="text/javascript">
<!--
function addDays(DIn,DOut,days) {
    // expecting dd/mm/yyyy
    var dateStrIn = document.getElementById(DIn).value;
    // change the string into a Date object
    var dateValueIn = new Date(dateStrIn.substring(6,10),dateStrIn.substring(3,5)-1,dateStrIn.substring(0,2));
    // One day equals 86400000 milliseconds
    var daysToAdd = parseInt(document.getElementById(days).value) * 86400000;
    // add the number of milliseconds to the Date object
    var dateValueOut = new Date(parseInt(dateValueIn.getTime())+daysToAdd);

    // month will be 0 .. 11
    if (parseInt(dateValueOut.getMonth()+1) < 10) {
        var monthOut = '0' + parseInt(dateValueOut.getMonth()+1);
    }else{
        var monthOut = parseInt(dateValueOut.getMonth()+1);
    }
    if (parseInt(dateValueOut.getDate())<10) {
        var dateOut = '0' + parseInt(dateValueOut.getDate());
    }else{
        var dateOut = parseInt(dateValueOut.getDate());
    }

    var dateStrOut = dateOut + '/' + monthOut + '/' + dateValueOut.getFullYear();
    document.getElementById(DOut).value=dateStrOut;
}
//-->
</script>
</head>

<body>
<form method="post" action="add-days-to-javascript-date.asp">
  Date In: <input type="text" id="dateIn" name="dateIn" value="19/07/2007"><br>
  Days To Add: <input type="text" id="daysToAdd" name="daysToAdd" value="7"><br>
  Date Out: <input type="text" id="dateOut" name="dateOut" value=""><br>
  <input type="button" value="Add Days To Date" onclick="addDays('dateIn','dateOut','daysToAdd');">
</form>
</body>

</html>

« Last Edit: July 10, 2007, 08:50:16 PM by rdivilbiss » Logged

Rod
fastrobby

Offline Offline

Posts: 19


« Reply #2 on: July 11, 2007, 09:07:34 PM »

Wow
Elegant code to say the least . .  It works seamlessly  . . You truly are an expert . .   What else can I say but thank you sooooooo much  . .
Robby
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« Reply #3 on: July 12, 2007, 06:08:57 PM »

Thanks. Nice to be appreciated.
Glad to help!

Logged

Rod
fastrobby

Offline Offline

Posts: 19


« Reply #4 on: July 14, 2007, 09:16:06 PM »

Hi  Rod,
I have one more little problem. Interoperability is essential in the form, i.e. the ability for the user to change dates or No of nights etc and get instant feed back from the calcMe routine.
Your routine is great like I said before  but now I cannot use mutiple instances of the calendar (I have many on one page)  because they all need a unique dateIn reference. Only way I have been able to make it work is if I stick a reference to the form into my case statement and then obviously all  interoperability is lost.

My attempts ( 0ne of many)
http://www.kohchangbookingandinformation.com/Resorts.asp

Clean version that works with one calendar
http://www.kohchangbookingandinformation.com/Resorts_2.asp
Do you have a solution ?
Thanks again 
« Last Edit: July 14, 2007, 09:30:32 PM by fastrobby » Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« Reply #5 on: July 14, 2007, 09:40:45 PM »

You have given the same name and id to multiple fields.

Just name them distinctly.

e.g. the first set could be input "dateIn1" with select daysToAdd1 and output field dateOut1, for the next section; would be input "dateIn2" with select daysToAdd2 and output field dateOut2, for the next section, etc. etc.

Then your corresponding onchange would be addDays('dateIn1','dateOut1','daysToAdd1'); and addDays('dateIn2','dateOut2','daysToAdd2');, etc. etc.

That simple.

Do I get a free weekend?  I'll be going to Vietnam next summer?  I could return via Bancock.

« Last Edit: July 14, 2007, 09:44:30 PM by rdivilbiss » Logged

Rod
fastrobby

Offline Offline

Posts: 19


« Reply #6 on: July 14, 2007, 10:01:07 PM »

Absolutely  . . and i hope you like beer  . .    I am trying your suggestion now but I have no concept of how it will work with dateIn and my calcMe func
« Last Edit: July 14, 2007, 10:33:38 PM by fastrobby » Logged
fastrobby

Offline Offline

Posts: 19


« Reply #7 on: July 15, 2007, 06:03:58 AM »

Hi Rod,
Well that brings me back full circle to my original problem.

thisform.adultPrice.value=roomType[thisform.roomType.selectedIndex];
thisform.totalPrice.value = parseInt(thisform.daysToAdd.value) * parseInt(thisform.adultPrice.value) * parseInt(thisform.adultNo.value);

daysToAdd is now unique within each  form and calcs but as you can see it's strict value cannot be passed by the routine above. I dont know how to pass the value and still keep the routine smooth. It needs to be totally interactive so that it appears seamless to the user. 

http://www.kohchangbookingandinformation.com/Resorts.asp

Thanks again
Robby
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« Reply #8 on: July 15, 2007, 09:30:39 AM »

I'll take a look a little later. I'm in the office and will not be able to check the code until 6-7PM CST.
Logged

Rod
fastrobby

Offline Offline

Posts: 19


« Reply #9 on: July 15, 2007, 11:08:25 AM »

Ok  . .  I'll be here  . .  Thanks
Robby
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« Reply #10 on: July 15, 2007, 09:08:13 PM »

First, while you may have multiple forms, you may not have multiple elements with the same name.

So the solution is to number the fields...e.g. dateIn1, dateOut1 ... etc.

http://www.rodsdot.com/ee/KOH_airline.asp

Then your CalcMe function can get the values this:
Code:
function CalcMe(arg1) {
var thisform = document.getElementById(arg1).id; // Gets ID of form
var thisFormNum = thisform.substr(5,1);

var ttlPrice = 0;
var cotPrice = 0;
var roomType = new Array();
switch (thisform){
case "Hotel1Form":
roomType[0] = "0";     //"Choose"
  roomType[1] = "400"; //"Room 1"
  roomType[2] = "400"; //"Room 2"
  roomType[3] = "600"; //"Room 3"
  roomType[4] = "600"; //"Room 4"
  roomType[5] = "4000"; //"Room 5"
  roomType[6] = "4000"; //"Room 6"
  roomType[7] = "4000"; //"Room 7"
  roomType[8] = "4000"; //"Room 8"
  roomType[9] = "4000"; //"Room 9"
  roomType[10] = "4000"; //"Room 10"
break;
case "Hotel2Form":
roomType[0] = "0";     //"Choose"
  roomType[1] = "400"; //"Room 1"
  roomType[2] = "400"; //"Room 2"
  roomType[3] = "600"; //"Room 3"
  roomType[4] = "600"; //"Room 4"
  roomType[5] = "4000"; //"Room 5"
  roomType[6] = "4000"; //"Room 6"
  roomType[7] = "4000"; //"Room 7"
  roomType[8] = "4000"; //"Room 8"
  roomType[9] = "4000"; //"Room 9"
  roomType[10] = "4000"; //"Room 10"
break;
case "Hotel3Form":
roomType[0] = "0";     //"Choose"
  roomType[1] = "400"; //"Room 1"
  roomType[2] = "400"; //"Room 2"
  roomType[3] = "600"; //"Room 3"
  roomType[4] = "600"; //"Room 4"
  roomType[5] = "4000"; //"Room 5"
  roomType[6] = "4000"; //"Room 6"
  roomType[7] = "4000"; //"Room 7"
  roomType[8] = "4000"; //"Room 8"
  roomType[9] = "4000"; //"Room 9"
  roomType[10] = "4000"; //"Room 10"
break;
case "Hotel4Form":
roomType[0] = "0";     //"Choose"
  roomType[1] = "400"; //"Room 1"
  roomType[2] = "400"; //"Room 2"
  roomType[3] = "600"; //"Room 3"
  roomType[4] = "600"; //"Room 4"
  roomType[5] = "4000"; //"Room 5"
  roomType[6] = "4000"; //"Room 6"
  roomType[7] = "4000"; //"Room 7"
  roomType[8] = "4000"; //"Room 8"
  roomType[9] = "4000"; //"Room 9"
  roomType[10] = "4000"; //"Room 10"
break;
case "Hotel5Form":
roomType[0] = "0";     //"Choose"
  roomType[1] = "400"; //"Room 1"
  roomType[2] = "400"; //"Room 2"
  roomType[3] = "600"; //"Room 3"
  roomType[4] = "600"; //"Room 4"
  roomType[5] = "4000"; //"Room 5"
  roomType[6] = "4000"; //"Room 6"
  roomType[7] = "4000"; //"Room 7"
  roomType[8] = "4000"; //"Room 8"
  roomType[9] = "4000"; //"Room 9"
  roomType[10] = "4000"; //"Room 10"
break;
}

var adultPriceField = document.getElementById('adultPrice'+thisFormNum);
var roomTypeField = document.getElementById('roomType'+thisFormNum);
var roomTypeSelected = roomTypeField.selectedIndex;

var totalPriceField = document.getElementById('totalPrice'+thisFormNum);
var daysToAddField = document.getElementById('daysToAdd'+thisFormNum);
var adultNoField = document.getElementById('adultNo'+thisFormNum);

adultPriceField.value = roomType[roomTypeSelected];
totalPriceField.value = parseInt(daysToAddField.value) * parseInt(adultPriceField.value) * parseInt(adultNoField.value);

try {
if(document.getElementById('cotPrice'+thisFormNum).type=='checkbox') {
    var cotPriceField = document.getElementById('cotPrice'+thisFormNum);
if( cotPriceField.checked==true) {
totalPriceField.value = parseInt(totalPriceField.value) + (parseInt(cotPriceField.value) * parseInt(daysToAddField.value));
}
}
} catch(e) {}
}

This allows multiple forms.
Logged

Rod
fastrobby

Offline Offline

Posts: 19


« Reply #11 on: July 15, 2007, 10:26:31 PM »

Hi,
I see your still up or were when I last looked. Your code is error free but mine has a roomtypeselected.index error which I will have to chase down.

http://www.kohchangbookingandinformation.com/Resorts.asp.

My first try at this routine was with variables but I couldn't nail them like you have. In fact I have avoided them throughout my site which I will now slowly go back and remedy Obviously two very different skill sets ;-)  This has been a great learning experience which I appreciate immensely. When do you propose to come to Asia ???

Thanks again
Robby
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« Reply #12 on: July 15, 2007, 10:38:14 PM »

June of 2008, if all goes well.
Logged

Rod
fastrobby

Offline Offline

Posts: 19


« Reply #13 on: July 16, 2007, 01:51:52 AM »

June is in  the rainy season but it's a trip. Different from the USA. Really really rains here. All sorts of thunder, lightning and incredible sunsets. You will love it. See Ya
Robby
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 424



WWW
« Reply #14 on: July 16, 2007, 05:34:27 PM »

I guess I was lucky last few times I went to VN...my June visit had little or no rain that I remember.  But May to October is supposed to be the rainy season there.

Do you work for the resorts or what?

As far as the code goes...you've got a lot of older JavaScript routines...for example the right-click disable function is useless and is written to be compatible with Netscape 4.xx, which should be many, many years extinct.  Similarly your remote scripting libraries really shouldn't be needed as more the 99% of the GUI browsers in use now support remote scripting without those work-arounds.

Of course, at this point you may have to leave some of those in to stop something else from breaking, but I'd take out the right click disable function...it offers no protection, is annoying, and is just something else that can mess up your page as it tinker's with the event handler.

On the original addDays function I wrote...I could have referenced the form fields, by using the form handle instead of using document.getElementById('xxx').  That way you could keep the field names the same on each form, but you would still need to have unique IDs as no two elements on the page can have the same ID.  If you do have two elements with the same ID, you'll get all kinds of strange problems with JavaScript attempting to access the DOM.  In a post or two before I said:

Quote
First, while you may have multiple forms, you may not have multiple elements with the same name.


e.g. said name when I meant ID.

Since you already had ID's and were posting each form to a different form handler, I chose to access the fields by ID.

Also, I think the array of room values is not necessary.  You can simply put those values into the select lists...for example:

Code:
<select name="roomType1" size="1" id="roomType1" onchange="CalcMe('Hotel1Form');">
                <option value="0" selected="selected">Choose Please</option>
                <option value="400">Regular Double Bed</option>
                <option value="400">Regular 2 Single Bed</option>
                <option value="600">Deluxe Double Bed w/Air</option>
                <option value="600">Deluxe 2 Single Bed w/Air</option>
</select>

If you don't default number of nights and number of guests fields to 1 instead of 0, then you'll get some error values in the price fields if people don't complete the field in the order you expect.

FWIW.
Logged

Rod
Pages: [1] 2
« previous next »
    Jump to: