Format a Date as a Year-Month-Day String in JavaScript

To format a date in YYYYMMDD or similar string format in JavaScript, we can use the following. In this example we use the current date.

Using dateformat:

$ npm install dateformat
const dateFormat = require('dateformat')

const now = new Date()
const dateString = dateFormat(now, 'yyyymmdd')

console.log(dateString)

Result:

20210313

Using moment.js:

$ npm install moment
const moment = require('moment')

const now = new Date()
const dateString = moment(now).format('YYYYMMDD')

console.log(dateString)

Result:

20210313

References

https://www.npmjs.com/package/dateformat

https://www.npmjs.com/package/moment

 

Make Commit Message Lint Git Hook Run First with Commitlint and Husky

Enforcing a standard for commit messages with a commit-msg hook using something like commitlint is useful, but ideally if we have a commit message hook set up to lint the commit message, it should run first, before any test suite.
That is, if we have a large unit test suite which must pass before committing is possible, and it takes a long time to run (i.e. a minute or two), it is better to have a commit message problem fail first, quickly. It is annoying to run through a large, slow test suite successfully only to have a commit message lint prevent the entire commit at the end!

It is not possible to change the order of git hooks execution. Git does not have access to the commit message at that stage of processing.

However, using commitlint and Husky, we can achieve the desired order by simply using the commit-msg hook to trigger our tests instead of the pre-commit hook.
Inside .huskyrc (or the “husky” field inside package.json) we can change this:

{
  "hooks": {
    "commit-msg": "commitlint -e $GIT_PARAMS",
    "pre-commit": "npm test"
  }
}

Into this:

{
  "hooks": {
    "commit-msg": "commitlint -e $GIT_PARAMS && npm test"
  }
}

The commit-msg hook will run before the commit is actually made, so this is still a pre-commit hook requiring successful run of the unit tests before one can commit.
But any problem with the commit message itself will show up immediately.

 

Change Commit Message Length Limit in Commitlint Config

Writing descriptive commit messages is helpful to readers of code; sometimes the default limit for the number of characters enforced by Commitlint in a given configuration is too short for descriptions of very specific changes. To change the character length limit do the following.
Suppose we are using an existing set of rules (in this example, the Angular config) and just want to change the character limit to 200.
The config file below should achieve this:

commitlint.config.js

module.exports = {
  extends: ['@commitlint/config-angular'],
  rules: {
    'header-max-length': [2, 'always', 200],
  }
}

This will use all of the rules from config-angular but override the commit message length rule (header-max-length).

The first element 2 means “throw error” (1 means “warning”, 0 means “disable the rule”).

References

https://commitlint.js.org/#/reference-rules