add 5 days to a string

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

add 5 days to a string

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: add 5 days to a string

Post 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.
Last edited by califdon on Wed Mar 09, 2011 11:56 am, edited 1 time in total.
Reason: Realized that this is Javascript!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: add 5 days to a string

Post 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);
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply