Date calculation
- Geir Mathisen
- Posts: 16
- Joined: Sun Dec 22, 2013
I need to set a default date field value based on another date filed in the form.
To_Date = From_Date + 10 days
Typically I would expect a javascript line something like this:
fd.field('To_Date').value(fd.field('From_Date').value()) + 10;
But this does not work.
Anyone know the correct syntax for doing date calculations in FD?
Thanks
To_Date = From_Date + 10 days
Typically I would expect a javascript line something like this:
fd.field('To_Date').value(fd.field('From_Date').value()) + 10;
But this does not work.
Anyone know the correct syntax for doing date calculations in FD?
Thanks
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Hello Geir,
First, you have to convert string representation of your date to JavaScript object. Please, try the following code:
Please, note that internal names of my fields are 'Start_x0020_Date' and 'End_x0020_Date', you should replace them with your values.
First, you have to convert string representation of your date to JavaScript object. Please, try the following code:
Code: Select all
fd.field('Start_x0020_Date').change(function() {
var startDateText = fd.field('Start_x0020_Date').value();
if (startDateText) {
var startDate = new Date(Date.parse(startDateText));
var endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 10);
fd.field('End_x0020_Date').value([endDate.getMonth()+1,
endDate.getDate(), endDate.getFullYear()].join('/'));
}
});
- Geir Mathisen
- Posts: 16
- Joined: Sun Dec 22, 2013
Thanks for yet another fast response.
I have tried to insert your code, with my variables.
Something goes wrong, which results in the calculated date not bbeing recognized as a date (or numbers).
Result = "NaN/NaN/NaN"
It seems to go wrong in this line:
var startDate = new Date(Date.parse(startDateText));
alert(startDate); - returns "InvalidDate".
Any idea what is causing this?
Thanks again
I have tried to insert your code, with my variables.
Something goes wrong, which results in the calculated date not bbeing recognized as a date (or numbers).
Result = "NaN/NaN/NaN"
It seems to go wrong in this line:
var startDate = new Date(Date.parse(startDateText));
alert(startDate); - returns "InvalidDate".
Any idea what is causing this?
Thanks again
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
What is your date format: mm/dd/yyyy? Do you use Date with Time fields or just Date?
- Geir Mathisen
- Posts: 16
- Joined: Sun Dec 22, 2013
Date fields is date only (no time).
Date format in Norway is: dd.mm.yyyy
(I replaced the pad char from "/" to ".")
Geir
Date format in Norway is: dd.mm.yyyy
(I replaced the pad char from "/" to ".")
Geir
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Could you place alert(startDateText) after this line fo code:
Code: Select all
var startDateText = fd.field('Start_x0020_Date').value();
- Geir Mathisen
- Posts: 16
- Joined: Sun Dec 22, 2013
Script:
var startDateText = fd.field('Fra_x0020_Dato').value();
alert(startDateText);
Returns
02.01.2014 - which is correct
var startDateText = fd.field('Fra_x0020_Dato').value();
alert(startDateText);
Returns
02.01.2014 - which is correct
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Parse function doesn't work for your date format (dd.mm.yyyy), please, try the following code instead:
Code: Select all
fd.field('Start_x0020_Date').change(function() {
var startDateText = fd.field('Start_x0020_Date').value();
if (startDateText) {
var startDateParts = startDateText.split('.');
var startDate = new Date(startDateParts[2], startDateParts[1]-1,
startDateParts[0]);
var endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 10);
fd.field('End_x0020_Date').value([endDate.getDate(), endDate.getMonth()+1,
endDate.getFullYear()].join('.'));
}
});
-
- Information
-
Who is online
Users browsing this forum: No registered users and 19 guests