Andrey
Andrey

Reputation: 11

How to use javascript function dateFormat for decades?

How to use Steven Levithan's dateFormat() function to format a date into decade? I have a date May 9th, 2019, 5:46:21 PM. I need to get something like "2010s". Should i create a new mask for it? Any suggestions?

Upvotes: 0

Views: 322

Answers (1)

jtate
jtate

Reputation: 2696

I'm unfamiliar with Steven Levithan's dateFormat() function, but you can do it with pure JS:

function getDecade(date) {
  return (Math.floor(date.getFullYear() / 10) * 10) + 's';
}

var today = new Date();
var nineties = new Date('1995-12-15');
var independenceDay = new Date('1776-07-04');

console.log(getDecade(today));
console.log(getDecade(nineties));
console.log(getDecade(independenceDay));

Upvotes: 2

Related Questions