Handlebar Help - Get the last day of the month

Comments

3 comments

  • Bas van Ditzhuijzen Strategic Partner NetSuite Integration Whiz Salesforce Integration Whiz
    Partner Implementation Certification
    Great Answer
    Answer Pro
    Engaged
    Celigo University Level 4: Legendary

    A nice trick is to take the first day of the next month and then substract one day. That would be like this:

    {{dateAdd (join "-" (timestamp "YYYY") (add (timestamp "MM") 1) "01") "-86400000"}}

    This wont work in december, so you'll need to do an compare for December and then set YYYY-12-31 in that case.

    1
  • Tyler Lamparter Principal Product Manager
    Awesome Follow-up
    Engaged
    Top Contributor
    Answer Pro
    Celigo University Level 4: Legendary

    Thao Dinh you could also just do some math do get what you need instead of needing to do a compare. The idea is to:

    • Get the first month of the current date. We know the first day of the month for the current date because it would be the current month and then 01.
    • After that, you add 32 days to the first date of the current month to get a date into the next month.
    • Then you get the first day of the next month.
    • Then you subtract 1 day to get the day before the first day of the next month which would be the same as the last day of the current month.
    {{dateFormat "YYYY-MM-DD" (dateAdd (dateFormat "YYYY-MM-01" (dateAdd (dateFormat "YYYY-MM-01" (timestamp) "YYYY-MM-DDTHH:mm:ss.SSS[Z]") "2769000000") "YYYY-MM-DDTHH:mm:ss.SSS[Z]") "-86400000") "YYYY-MM-DDTHH:mm:ss.SSS[Z]"}}

    1
  • Thao Dinh

    thank you both!

    Tyler Lamparter this works great!

    0

Please sign in to leave a comment.