Calculate Dates
Hello,
I want to calcalute a date and I have some problem with it.
Example 1:
I have a field of the Type Date
I want to work with this date for example calculate + 1 Year or give it a new format like dd.mm.yyyy
but it don't work with this.
var date = new Date();
var newdate = new Date([[Start_x0020_date]]);
newdate.setDate(newdate.getDate());
var dd = newdate.getDate();
var mm = newdate.getMonth() +1;
var y = newdate.getFullYear();
var someFormattedDate = y + '/' + '0'+mm + '/' + dd;
return someFormattedDate;
What is wrong?
Example 2:
I have a form with a date field "Created" (=[[Erstelldatum]]). In the list settings I have set that per default the value should be today.
I have a seconde date field "Expire Date". I want to calculate the Expire Date in Rich Forms --> Behaviour with a Function Code.
I have done it with this code.
var str = [[Erstelldatum]];
var array = str.split(".",3);
var date= array[2]+"-"+array[1]+"-"+array[0];
var newdate = new Date(date);
newdate.setDate(newdate.getDate()+730 );
var dd = newdate.getDate();
var mm = newdate.getMonth() + 1;
var y = newdate.getFullYear();
var FullDate = dd + '.' + mm + '.' + y;
return FullDate;
Why I have gone this way? [[Erstelldatum]] = 08.02.2019 and I get an error doing this
var newdate = new Date([[Erstelldatum]])
is there a much more easier way to do the calculation?
how can I easily format date fields in skybow?
Kind regards
Nicole
-
Hi Nicole Geier,
you could use moment.js which is much easier and regardless of the culture.
Reading JavaScript date values from SharePoint date fields regardless of locale (culture)
Kind regards
Matthias
0 -
Hi Matthias,
if I insert the Script von Adis I get this error:
Should I insert the moment.js in the SiteAssets and change to var url="mySiteAssets"?
Kind regards
Nicole
0 -
Hi Matthias Walter,
sorry I didn't mention you in my answer. I couldn't load the framework moment.js should upload it to the site collection?
Kind regards
Nicole
0 -
Hi Nicole Geier
Please check if you have connection to the moment library with the client by typing libraries browser address. If download of moment.js is shown you can load it as you did. If it's not available this way you have to provide it somewhere in your environment and link to this.
0 -
I got the connection to this link as shown in your mail. I have also download the file into my SiteAssets
but you see the functio moment isn't recognized. So I couldn't get a date out of it.
Kind regards
Nicole
0 -
Hi Nicole Geier,
'moment' is not defined in Expression Builder as it's not initialized same as in runtime. You can ignore this message in Expression Builder.
Note the Function code expression type expect a return in the code block.
May the Test without a selected item could be also an issue in your test.
0
Please sign in to leave a comment.
Comments
6 comments