Page 1 of 2

update date field

Posted: Mon Nov 12, 2012 12:34 pm
by johan
Hi,

In my form I use 2 date fields. start and end.
Start = user input.
End : has to be start + 3 weeks.

How can I do this with javascript?

Thanks,
Johan

Re: update date field

Posted: Mon Nov 12, 2012 2:29 pm
by zazzium
Hi Johan,
I recommended this js date library http://www.datejs.com/

Re: update date field

Posted: Mon Nov 12, 2012 4:25 pm
by johan
Zazzium,

Thanks for your reply.

I've tried it with

Code: Select all

// Get a date 30 days after a user supplied date
var d1 = Date.parse('start');
var d2 = (30).days().after(d1);
alert(d2);
Then I get NaN so seems I'm doing something wrong.
Any idea?

Johan

Re: update date field

Posted: Mon Nov 12, 2012 7:37 pm
by zazzium
First u have to convert the date to something js can handle.
if u have date format like yyyy-mm-dd u can make function like:

Code: Select all

function convertJsDate(convert_date) //format yyyy-mm-dd
{
    date_split = convert_date.split('-');
    convertDate = date_split[0]+"/"+date_split[2]+"/"+date_split[1]; 
    return convertDate; //returns yyyy/dd/mm
}

var start="2012-11-12";
var start_date  = convertJsDate(start);
var end_date = new Date(start_date).addWeeks(3).toString("yyyy-M-dd"); //adds 3weeks and converts date to yyyy-mm-dd

alert(end_date);
if u have differente date format for 'start' u have to change the function accordingly

datejs proper APIDocumentation is here:
http://code.google.com/p/datejs/wiki/APIDocumentation

hope it helps

Re: update date field

Posted: Tue Nov 13, 2012 6:31 am
by johan
Zazzium,

I'm trying to let your code work. This is what I made of it

Code: Select all

function convertJsDate(convert_date) //format dd-mm-yyyy
{
    date_split = convert_date.split('-');
    convertDate = date_split[0]+"-"+date_split[1]+"-"+date_split[2];
    return convertDate; //returns dd/mm/yyyy

}
function eind_datum(){
var start= document.getElementById('start').value;
var start_date  = convertJsDate(start);

var end_date = new Date(start_date).addWeeks(3).toString("dd-mm-yyyy"); //adds 3weeks and converts date to yyyy-mm-dd; //adds 3weeks and converts date to dd-mm-yyyy

alert (end_date);
}
alert(end_date) does'nt work. Step by step I see that alert(start_date) returns the correct date filled in 'start' on my form. Seems I"m doing something wrong in var end_date. Problem is I don't see what I'm doing wrong :(

Re: update date field

Posted: Tue Nov 13, 2012 9:49 am
by zazzium
Try this (just copy paste)

Code: Select all

function convertJsDate(convert_date) //format dd-mm-yyyy
{
    date_split = convert_date.split('-');
     fromDate = insert_split[1]+"/"+insert_split[0]+"/"+insert_split[2];
    return convertDate;
}

function eind_datum(){
var start= document.getElementById('start').value;
var start_date  = convertJsDate(start);

var end_date = new Date(start_date).addWeeks(3).toString("dd-M-yyyy");

alert (end_date);
}

Re: update date field

Posted: Tue Nov 13, 2012 4:16 pm
by johan
Zazzium,

Thanks for your reply but this code does'nt work.

I've tried alert(end_date) - alert (start_date). Non of these gives me an alert.

Johan

Re: update date field

Posted: Tue Nov 13, 2012 4:36 pm
by zazzium
sry, my bad (i copied my code line with the wrong var's)

Code: Select all

function convertJsDate(convert_date) //format dd-mm-yyyy
{
    var date_split = convert_date.split('-');
    var convertDate = date_split[1]+"/"+date_split[0]+"/"+date_split[2];
    return convertDate;
}

Re: update date field

Posted: Tue Nov 13, 2012 4:49 pm
by johan
Zazzium,

When I change this I get start_date = 11/13/2012 (m/dd/yyyy) My input from start = dd/mm/yyyy

That's why I've changed var convertDate = date_split[1]+"/"+date_split[0]+"/"+date_split[2]; to var convertDate = date_split[0]+"/"+date_split[1]+"/"+date_split[2];

alert(end_date) still doesn't work.

Johan

Re: update date field

Posted: Tue Nov 13, 2012 8:05 pm
by zazzium
js date format has to be mm/dd/yyyy (or yyyy/mm/dd )

btw, it's a silly question, but did u downloaded and included the datejs library (date.js) file?