Date calculation

rss

Posted by Geir Mathisen - Jan 20 ’14 at 10:13

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

 

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 9 years 8 months 16 days
    #1 by Dmitry Kozlov Jan 21 ’14 at 07:36

    Hello Geir,

    First, you have to convert string representation of your date to JavaScript object. Please, try the following code:

    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('/'));
    	}
    }); 
    

    Please, note that internal names of my fields are 'Start_x0020_Date' and 'End_x0020_Date', you should replace them with your values.

  • Geir Mathisen
    Geir Mathisen
    Member
    Member for: 9 years 5 months 11 days
    #2 by Geir Mathisen Jan 21 ’14 at 02:54

    [#1]:

    [#1]: 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

  • Dmitry
    Dmitry
    Admin
    Member for: 10 years 11 months 28 days
    #3 by Dmitry Jan 22 ’14 at 04:58

    [#2]: What is your date format: mm/dd/yyyy? Do you use Date with Time fields or just Date?

  • Geir Mathisen
    Geir Mathisen
    Member
    Member for: 9 years 5 months 11 days
    #4 by Geir Mathisen Jan 22 ’14 at 07:03

    [#3]: Date fields is date only (no time).

    Date format in Norway is: dd.mm.yyyy

    (I replaced the pad char from "/" to ".")

    Geir

  • Dmitry
    Dmitry
    Admin
    Member for: 10 years 11 months 28 days
    #5 by Dmitry Jan 22 ’14 at 07:19

    [#4]: Could you place alert(startDateText) after this line fo code:

    var startDateText = fd.field('Start_x0020_Date').value();
    
  • Geir Mathisen
    Geir Mathisen
    Member
    Member for: 9 years 5 months 11 days
    #6 by Geir Mathisen Jan 22 ’14 at 08:22

    [#5]: Script:

    var startDateText = fd.field('Fra_x0020_Dato').value();
    alert(startDateText);
    Returns

    02.01.2014 - which is correct

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 9 years 8 months 16 days
    #7 by Dmitry Kozlov Jan 23 ’14 at 02:58

    [#6]: Parse function doesn't work for your date format (dd.mm.yyyy), please, try the following code instead:

     
    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('.'));
    	}
    });
    
  • Geir Mathisen
    Geir Mathisen
    Member
    Member for: 9 years 5 months 11 days
    #8 by Geir Mathisen Jan 23 ’14 at 03:26

    [#7]: That worked perfectly.

    TAHNKS A LOT!

  • JorgeM00
    Member
    Member for: 6 months 10 days
    #9 by JorgeM00 Nov 28 ’22 at 05:16

    Date calculation: Why you want it, how to get it, what are your options. Date calculation is something that everyone needs to know how to do. This article covers the basics of date calculation and provides some useful links for those looking for more information about this subject. Lets get briansclub and manage your new crypto business easily. But since we don't have a calendar to tell us the date of each day, we need to find another way to get an idea of what day it is. Luckily for us there is one way available – date calculation.

  • Rowan943
    Member
    Member for: 22 days
    #10 by Rowan943 May 20 ’23 at 03:02

    [#9]:  While it's challenging to pinpoint a single area as the absolute cheapest to buy cars, there are several regions that tend to offer more affordable options. Let's explore some of these areas and the reasons behind their affordability.https://www.youtube.com/watch?v=EwtBFu9WAh0

Displaying 1 to 10 of 11 messages
Previous12