Skip to content

Instantly share code, notes, and snippets.

@NickDeckerDevs
Last active February 14, 2023 17:06
Show Gist options
  • Save NickDeckerDevs/faf4ea2b90e173a27e2e2760f19e0eec to your computer and use it in GitHub Desktop.
Save NickDeckerDevs/faf4ea2b90e173a27e2e2760f19e0eec to your computer and use it in GitHub Desktop.
years months since date in javascript hubspot
// this will export out a 8 years 6 months since date
exports.main = async (event, callback) => {
// you can change these to yr, yrs, mo, mos if you would like
const terms = {
year: 'year',
yearPlural: 'years',
month: 'month',
monthPlural: 'months'
}
const { originalDate } = event.inputFields
const timestamp = Number(moveInDate)
const date = new Date(timestamp)
// console.log('date: ', date)
const currentDate = new Date()
// console.log('currentDate: ', currentDate)
const diffTime = currentDate - date
// console.log('diffTime: ', diffTime)
const diffYears = Math.floor(diffTime / (1000 * 60 * 60 * 24 * 365.25))
// console.log('diffYears: ', diffYears)
const diffMonths = Math.floor(diffTime / (1000 * 60 * 60 * 24 * 30.44))
// console.log('diffMonths: ', diffMonths)
const timeSinceDate = diffYears + " " + (diffYears === 1 ? terms.year : terms.yearPlural) + " and " + diffMonths + " " + (diffMonths === 1 ? terms.month : terms.monthPlural)
callback({
outputFields: {
timeSinceDate
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment