Page 1 of 1

add 5 days to a string

Posted: Tue Mar 08, 2011 6:21 pm
by pedroz
I have this string
var start = $('[name=invoicedate]').val();
alert('start'); // 2010/01/01

how can I add five days to have the following?
alert('start'); // 2010/01/10

Tried
var start = new Date(year, month, day+5);
but seems not work

Re: add 5 days to a string

Posted: Tue Mar 08, 2011 8:14 pm
by califdon
You might use strtotime().
http://www.w3schools.com/php/func_date_strtotime.asp

[Edit:] Oops, I wasn't paying attention. You can use PHP's strtotime() function if you are building the Javascript using PHP, but of course not if you must do this after the page has been sent to the browser! Sorry for my hasty answer.

Re: add 5 days to a string

Posted: Wed Mar 09, 2011 4:49 am
by VladSun
First we extend the Date object:

Code: Select all

Date.prototype.addDays = function (days)
{
	this.setDate(this.getDate() + days);
}
Then use addDays whenever you want:

Code: Select all

var start = '2010/01/31';

startDate = new Date(start);
startDate.addDays(5);
alert(startDate);