forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.66k stars 339 forks source link

Use moment to format dates #378

Closed Sharcoux closed 4 years ago

Sharcoux commented 4 years ago

Forgive me if my question sounds stupid, but how can I use momentJS to format dates in an email template?

I wish I could use something like:

b #{moment(date).format('dddd Do MMM YYYY à HH:mm:ss')}

I'd prefer doing this in the email because I think that formating the date and deciding about how to render the information is something that should belong to the template and not the method that would send the email.

What is the way to proceed?

TomasVink commented 4 years ago

This should work.

const moment = require('moment')

const someDate = new Date()

email
  .send({
    template: 'mars',
    message: {
      to: 'elon@spacex.com'
    },
    locals: {
      moment: moment,
      date: someDate
    }
  })
  .then(console.log)
  .catch(console.error);
// html.pug

p Display year of date: #{moment(date).format('YYYY')}