PDA

View Full Version : Get Date Object from text box



ElmoWatson
06-03-2014, 05:05 PM
I've got a textbox, the results of which I've turned into a variable:

question1=document.getElementsByName("RequestDate")[0].value)

all examples of creating a date object say:
myObject= New Date();

I need to create a date object out of 'question1' so I can then add 1 day to it.

How can I do this?

Philip M
06-03-2014, 05:38 PM
I've got a textbox, the results of which I've turned into a variable:

question1=document.getElementsByName("RequestDate")[0].value)

all examples of creating a date object say:
myObject= New Date();

I need to create a date object out of 'question1' so I can then add 1 day to it.

How can I do this?

You do not tell us the format of the date entered into the textbox.
And do you need to validate that it is a valid date, not 56th Febtember?

BTW, it is new Date(), not New Date() as Javascript is case sensitive.

Quizmaster: New Zealand was mapped out in the 1770s by which British Explorer?
Contestant: Robinson Crusoe

ElmoWatson
06-03-2014, 05:43 PM
normal format would be:
6/5/2014
I realize that (new) - - I typed it off the top of my head....

Philip M
06-03-2014, 05:48 PM
normal format would be:
6/5/2014
I realize that (new) - - I typed it off the top of my head....

Alas. Is 6/5/2014 supposed to be 6th of May or 5th June?
Do you want to validate the date (but of course UK and US users will intend different dates)?

Try this:-


<script type = "text/javascript">

function addDays(dt){
var ds = dt.split("/");
var startDate = new Date(ds[2], ds[1]-1, ds[0]); // YYYY/MM/DD - months in Javascript are 0-11
var daysAhead = 7; // change to 6 for days inclusive of start date
startDate.setTime(startDate.getTime()+(daysAhead*24*60*60*1000));
var futureDate = startDate.getDate()+"/"+(startDate.getMonth()+1)+"/"+startDate.getFullYear();
futureDate = futureDate.replace(/^(\d{1}\/)/,"0$1").replace(/(\d{2}\/)(\d{1}\/)/,"$10$2"); // add leading zeroes
alert("End Date = " + futureDate);
}

addDays("6/5/2014"); // 6th May 2014

</script>

ElmoWatson
06-03-2014, 05:55 PM
Now that I realize you're from London, that is a valid question.
Here in US, it's Month/Day/Year

This is a local intranet - the JQuery DatePicker is putting the date into the textbox, so at this point in development, I'm not even worried about validation yet.

Philip M
06-03-2014, 06:02 PM
Just change this line to

var startDate = new Date(ds[2], ds[0]-1, ds[1]); // YYYY/MM/DD - months in Javascript are 0-11

ElmoWatson
06-03-2014, 06:29 PM
thanks for the effort, but I now get 5 months and 2 days in the future.
I have (for instance) today: 6/3/2014
I want 6/4/2014
I'm getting 10/5/2014

Here's my code:
function addDays(dt)
{
var ds = dt.split("/");
//var startDate = new Date(ds[2], ds[1]-1, ds[0]); // YYYY/MM/DD - months in Javascript are 0-11
var startDate = new Date(ds[2], ds[0]-1, ds[1]);
var daysAhead = 7; // change to 6 for days inclusive of start date
startDate.setTime(startDate.getTime()+(daysAhead*24*60*60*1000));
var futureDate = startDate.getDate()+"/"+(startDate.getMonth()+1)+"/"+startDate.getFullYear();
futureDate = futureDate.replace(/^(\d{1}\/)/,"0$1").replace(/(\d{2}\/)(\d{1}\/)/,"$10$2"); // add leading zeroes
alert("End Date = " + futureDate);
return futureDate;
}
var question1=addDays(document.getElementsByName("RequestDate")[0].value);

chrishirst
06-03-2014, 06:46 PM
the JQuery DatePicke

So why do you need to convert from a text box when you could use the date object directly?

xelawho
06-03-2014, 06:50 PM
it's because your futureDate variable is using dd/mm/yyyy format (and your daysAhead is still set to 7). Change daysAhead to 1 and the var futureDate line to this:


var futureDate = (startDate.getMonth()+1)+"/"+startDate.getDate()+"/"+startDate.getFullYear();

Philip M
06-03-2014, 06:51 PM
<script type = "text/javascript">

function addDays(dt){
var ds = dt.split("/");
var startDate = new Date(ds[2], ds[0]-1, ds[1]); // YYYY/MM/DD - months in Javascript are 0-11
var daysAhead = 7; // change to 6 for days inclusive of start date
startDate.setTime(startDate.getTime()+(daysAhead*24*60*60*1000));
var futureDate = startDate.getMonth()+1 +"/"+startDate.getDate()+"/"+startDate.getFullYear();
futureDate = futureDate.replace(/^(\d{1}\/)/,"0$1").replace(/(\d{2}\/)(\d{1}\/)/,"$10$2"); // add leading zeroes
alert("End Date = " + futureDate);
}

addDays("07/05/2014"); // month, day, year (5th July 2014)

</script>

Obviously if you only want one day ahead you can simplify this code.

startDate.setTime(startDate.getTime()+(24*60*60*1000)); // one day ahead

rnd me
06-03-2014, 07:12 PM
Obviously if you only want one day ahead you can simplify this code.

startDate.setTime(startDate.getTime()+(24*60*60*1000)); // one day ahead

actually, it would be simpler just to increment the day and skip all the math.


JS is smart about handling stuff like "feb 30th": new Date(2014, 1, 30); // == Sun Mar 02 2014



in related news, i just figured out an easy way to find the 90th day in 2014: new Date(2014, 0, 90); which is == to Mon Mar 31 2014, since jan+mar have 31 days to counter feb's 28...
i've verified that this "trick" works in chrome, firefox and ie8+...

Philip M
06-03-2014, 08:19 PM
actually, it would be simpler just to increment the day and skip all the math.


Simpler but no formatting.



<script type = "text/javascript">

function addOneDay(dt){
var ds = dt.split("/");
var nextDate = new Date(ds[2], ds[0]-1, Number(ds[1])+1); // YYYY/MM/DD - months in Javascript are 0-11
alert("Tomorrow = " + nextDate);
}

addOneDay("07/31/2014"); // month, day, year (31st July 2014 USA format)

</script>

ElmoWatson
06-03-2014, 08:36 PM
Thanks for all the help.
Between XelaWho & Philip, I got it working just fine.