Using moment helper in assemble.io

181 Views Asked by At

I'm having a problem with the moment js using the assemble.io helpers. The software we use allows us to use these helpers to customize our documents. I'm trying to add days to a date. I want our invoice to add 30 days to the invoice date as the due date. Here is what I've tried:

InvoiceDate is the var that the software uses to pull in the date.

{{moment InvoiceDate add="30, 'd'" format="MMM DD, YYYY"}}
{{moment InvoiceDate add="30, 'days'" format="MMM DD, YYYY"}}

What am I doing wrong?

http://assemble.io/helpers/helpers-dates.html https://momentjs.com/docs/

1

There are 1 best solutions below

2
doowb On

From looking at the README.md examples for the moment helper, the value being passed to the add property needs to be an object with the key being the granularity ("days") and the value being the amount (30).

You can't format an object as a literal in Handlebars, so the data will need to be passed in somehow. If you're using software that handles frontmatter, then you can do something like this:

---
modifiers:
  invoiceDate:
    days: 30
---
{{moment InvoiceDate add=modifiers.invoiceDate format="MMM DD, YYYY"}}

Otherwise, you'll have to somehow include the modifier on your context for Handlebars to see:

let fn = Handlebars.compile('{{moment InvoiceDate add=modifiers.invoiceDate format="MMM DD, YYYY"}}');
let data = {
  InvoiceDate: new Date(),
  modifiers: {
    invoiceDate: {
      day: 30
    }
  }
};

console.log(fn(data));