Created
February 2, 2023 09:46
-
-
Save tobiasroeder/0aeab6cc81a68016a6c1d9a79cc81187 to your computer and use it in GitHub Desktop.
Custom date format prototype.
This file contains 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
/** | |
* Custom date format prototype. | |
* | |
* @param {string} format Format character inspired by PHP. @link https://www.php.net/manual/en/datetime.format.php | |
* | |
* @returns {string|Date} | |
*/ | |
Date.prototype.format = function (format) { | |
const to2digits = n => (n < 10 ? '0' + n : n); | |
if (format === 'w') { | |
return `${this.getFullYear()}-${to2digits( | |
this.getMonth() + 1 | |
)}-${to2digits(this.getDate())}`; | |
} | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment