Jump to content

Calculate EndDate from Field "Month" and "StartDate" for EU?


Martin

Recommended Posts

Hello,

 

I am just a starter with JS, but I am trying to fill a Field with a correct Rule in FP. This I would like to use in MarcomCentral for setting up a Version Product for contracts.

I did set-up the Field "Months" and two other Fields for "Start Date" and "End date". I would like to use the calendar in MC to fill in the start date, and it will be in the format dd-mm-yyyy.

For example the Field Months need to be filled as an open filled or drop down with 36, 48 or 60.

 

I did setup a rule like below, but it is totally not correct. The part of showing the right format is wrong, but more problems with correct calculation.

 

What am I doing wrong?

I think a lot, but please help:p

 

var s = Field("Start Date");

var myDate = new DateFromString(Field("Date"));

myDate.setMonth(myDate.getMonth() + Field("Months"));

return FormatDate(myDate, "dd/mm/yyyy");

Link to comment
Share on other sites

HI am just a starter with JS, but I am trying to fill a Field with a correct Rule in FP. This I would like to use in MarcomCentral for setting up a Version Product for contracts.

I did set-up the Field "Months" and two other Fields for "Start Date" and "End date". I would like to use the calendar in MC to fill in the start date, and it will be in the format dd-mm-yyyy.

For example the Field Months need to be filled as an open filled or drop down with 36, 48 or 60.

I'm afraid I don't understand what you're trying to accomplish from that description. Can you maybe provide a couple of example records of data with the relevant fields and the expected result for each?

I did setup a rule like below, but it is totally not correct. The part of showing the right format is wrong, but more problems with correct calculation.

 

What am I doing wrong?

I think a lot, but please help:p

Okay, let's start at the beginning:

var s = Field("Start Date");

I don't see what effect that first line has on the rest of the code. You're not using that "s" variable at all after setting it.

 

Are you trying to modify the date in that "Start Date" field or the date in the "Date" field? If there's an "End Date" field, why not use that?

 

As for the rest:

var myDate = new DateFromString(Field("Date"));

myDate.setMonth(myDate.getMonth() + Field("Months"));

return FormatDate(myDate, "dd/mm/yyyy");

If you're trying to simply add the number of Months in that field to the date in the Date field, that should work. Although I would explicitly convert the field value to a number first:

var myDate = new DateFromString(Field("Date"));
myDate.setMonth(myDate.getMonth() + Int(Field("Months")));
return FormatDate(myDate, "dd/mm/yyyy");

But basically, those last three lines of code seem okay. I just don't know what different result you're expecting than what you're getting now.

Link to comment
Share on other sites

Hello Dan, thanks for the reply and time you spend!

The reason that there are lines who doesn't make sense is because the less knowledge I have of JS. With a lot of searching on the forum and copying and pasting I did came with that terrible result.

The only small thing that is still a struggle and I do not know how to change, is the result date. When I use the input Field with 12-03-2017 (is 12 March 2017) and the months Field with 12, the result is showing 12-03-2018 as December-03-2018. How do I change this in the format 12-03-2018(12 March 2018)?

P.S. Just a quick question for a suggestion... Is for learning JS, reading the JavaScript Guide the most logic way or do you have any smart tips? Thanks!!

Edited by Martin
Link to comment
Share on other sites

When I use the input Field with 12-03-2017 (is 12 March 2017) and the months Field with 12, the result is showing 12-03-2018 as December-03-2018. How do I change this in the format 12-03-2018(12 March 2018)?

I think the DateFromString function is anticipating the input being in month-day-year format. You can give this a try:

var [d,m,y] = Field("Date").split("-").map(Int);
var myDate = new Date(y, m + Int(Field("Months") - 1), d);
return FormatDate(myDate, "dd lm yyyy");

P.S. Just a quick question for a suggestion... Is for learning JS, reading the JavaScript Guide the most logic way or do you have any smart tips? Thanks!!

FusionPro has many built in functions and callbacks that are specific to the FusionPro application, so outside of this forum, it might be difficult to find how to code a FP-specific solution. That being said, anyone who uses FP could certainly benefit from understanding the JavaScript basics and syntax. Taking a course online or doing tutorials is a great way to familiarize yourself with JavaScript and will help you better understand code posted to this forum.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...