Created
February 24, 2019 14:35
-
-
Save yuriitaran/8d87d316a8ed22a18a3e99d52bca9fef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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