Home > Mobile >  How to separate month and year in Date in pug
How to separate month and year in Date in pug

Time:01-26

This is the format of date saved in MongoDB:

Sun Feb 13 2022 05:30:00 GMT 0530 (India Standard Time)

How to separate month, year and day from this date in pug? I want to take this date in pug and display month and year separately.

CodePudding user response:

Pug is built on JavaScript, so you could just use string manipulation to parse the month, day, and year—assuming the format is consistent.

-
  // variable containing date string
  const myString = "Sun Feb 13 2022 05:30:00 GMT 0530 (India Standard Time)"
  // break into individual variables using destructuring
  let [dayOfWeek, month, day, year, time, timeZone, timeZoneName] = myString.split(' ')

p.postDate Posted on #{month} #{day}, #{year}

CodePudding user response:

Thanks everyone for your help, i found a npm library called moment.js. It is super easy to use and also has many built in functions.

  •  Tags:  
  • Related