Date calculation error
With the following code I would like to check how long ago a date was and get a text "a year ago" (in German "vor einem Jahr") as an output: var zeit = moment(moment(@All of Date.toString()).format("DD.MM.YYYY")).lang('de').fromNow()var cat = @All of Statuscat == "Aktiv" ? zeit : 0 Yet, I get the error message: Invalid value "vor einem Jahr" (string): must be number How can I fix this? I already tried setting up a new calculation field, but always get the same error message...

  • Yes, this is exactly what I was looking for! Just had to make one small adjustment regarding one line, this is how it works now: if(cat[i] == "Aktiv"){
  • I assume @all of Status pulls the status from the same items as @all of Date pulls the dates from. So for example: if the item with Date "Sun Jan 01 2015 00:00:00 GMT+0000 (UTC)" isn't "Aktiv" - you don't want it as latest items. Right? There are multiple ways you can get the result. E.g. this one: var zeit = @All of Date;var cat = @All of Status;var datesFound = [];for(var i = 0; i < zeit.length; i++){if(cat[i] == "Aktiv"){;datesFound.push(moment(zeit[i]).format("YYYYMMDD"));}};var result = datesFound.sort().reverse();moment(result[0],"YYYYMMDD").fromNow()
  • Thanks Rainer, "0" works perfectly.  However, I just discovered that I need to link more than 1 item, so @All of Date would give me something like  Sun Jan 01 2006 00:00:00 GMT+0000 (UTC),Sun Jan 01 2015 00:00:00 GMT+0000 (UTC),Sun Jan 01 2014 00:00:00 GMT+0000 (UTC) Is it possible to write code that gets just the latest date (in this case "Sun Jan 01 2015 00:00:00 GMT+0000 (UTC)") from the array above?
  • Hi Robert, you normally don't have to care for that error msgs. like:  Invalid value ... must be number/string/text/date.If you can save the template and it shows the right result in the item everything is ok.  The calculation sandbox (which checks if your code is ok) is very sensitive. In this case it expects a number cause you use " : 0" (and 0 is a number). In an item where cat != "Aktiv" the calc field would be empty, it'wont show 0 - cause it's a number but your calc field is formatted as text type field.  Try : "0" (so number is a string) if you need to show a zero, or "" (shows nothing = empty field).  Rainer
This question is closed.