Skip to content

Instantly share code, notes, and snippets.

@yuriitaran
Created February 24, 2019 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuriitaran/8d87d316a8ed22a18a3e99d52bca9fef to your computer and use it in GitHub Desktop.
Save yuriitaran/8d87d316a8ed22a18a3e99d52bca9fef to your computer and use it in GitHub Desktop.
// Attaching a new function toShortFormat() to any instance of Date() class
Date.prototype.toShortFormat = function() {
const months = ["Jan","Feb","Mar", "Apr","May","Jun", "Jul","Aug","Sep", "Oct","Nov","Dec"];
const day = this.getDate();
const monthIndex = this.getMonth();
const year = this.getFullYear();
return "" + day + "-" + months[monthIndex] + "-" + year;
}
// Now any Date object can be declared
const today = new Date();
// and it can represent itself in the custom format defined above.
console.log(today.toShortFormat()); // current date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment