Skip to content

Instantly share code, notes, and snippets.

@tobiasroeder
Created February 2, 2023 09:46
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 tobiasroeder/0aeab6cc81a68016a6c1d9a79cc81187 to your computer and use it in GitHub Desktop.
Save tobiasroeder/0aeab6cc81a68016a6c1d9a79cc81187 to your computer and use it in GitHub Desktop.
Custom date format prototype.
/**
* 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