Welcome to the nuBuilder forums!

Please register and login to view forums and other content only available to registered users.

update date field

johan
Posts: 392
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium

update date field

Unread post 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
zazzium
Posts: 84
Joined: Mon Jul 04, 2011 12:52 am

Re: update date field

Unread post by zazzium »

Hi Johan,
I recommended this js date library http://www.datejs.com/
johan
Posts: 392
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium

Re: update date field

Unread post 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
zazzium
Posts: 84
Joined: Mon Jul 04, 2011 12:52 am

Re: update date field

Unread post 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
johan
Posts: 392
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium

Re: update date field

Unread post 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 :(
zazzium
Posts: 84
Joined: Mon Jul 04, 2011 12:52 am

Re: update date field

Unread post 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);
}
johan
Posts: 392
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium

Re: update date field

Unread post 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
zazzium
Posts: 84
Joined: Mon Jul 04, 2011 12:52 am

Re: update date field

Unread post 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;
}
johan
Posts: 392
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium

Re: update date field

Unread post 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
zazzium
Posts: 84
Joined: Mon Jul 04, 2011 12:52 am

Re: update date field

Unread post 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?
Locked