release-notes-generator icon indicating copy to clipboard operation
release-notes-generator copied to clipboard

Changing the date format

Open bilelomrani1 opened this issue 5 years ago • 3 comments

I wanted to change the date format, next to each release. The default format is yyyy-mm-dd. I looked at conventional-changelog-writer properties and saw that there is a transform method that formats committerDate with yyyy-mm-dd. But because it's a function, I don't really know how am I supposed to hook into it from my .releaserc file.

Looking at the source code of conventional-changelog-writer, the default transform method is

options.transform = _.assign({
      hash: function (hash) {
        if (_.isString(hash)) {
          return hash.substring(0, 7)
        }
      },
      header: function (header) {
        return header.substring(0, 100)
      },
      committerDate: function (date) {
        if (!date) {
          return
        }

        return dateFormat(date, 'yyyy-mm-dd', true)
      }
    }, options.transform)

I tried the following but it fails.

branches:
  - master
plugins:
  - - "@semantic-release/commit-analyzer"
    - preset: conventionalcommits
  - - "@semantic-release/release-notes-generator"
    - preset: conventionalcommits
      writerOpts:
        date: dateFormat(new Date(), 'yyyy/mm/dd', true)
        transform: |
          _.assign({
            hash: function (hash) {
              if (_.isString(hash)) {
                return hash.substring(0, 7)
              }
            },
            header: function (header) {
              return header.substring(0, 100)
            },
            committerDate: function (date) {
              if (!date) {
                return
              }

              return dateFormat(date, 'yyyy/mm/dd', true)
            }
          }, options.transform)

bilelomrani1 avatar Sep 22 '20 23:09 bilelomrani1

Did you solve your problem?

Currently, my CI builds create the date in the header in the following format: mm/dd/yyyy I don't know why but it seems to be related to the locale settings of the container. On my Mac it is still yyyy-mm-dd.

It would be nice to use a dedicated setting for the format or a hint in the documentation how to handle date format issues.

sac-cas avatar Feb 16 '24 11:02 sac-cas

It seems like date can't be configured at all?

The config value https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-writer#date is outside of the writerOpts.

My config currently produces dates formatted like 2024-2-27 🤷

Edit: Found this function called formatDate https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-writer/src/types/options.ts#L116, unfortunately it is never called...

Edit 2: I ended up using pnpm patch conventional-changelog-writer to fix the formatting.

patches/[email protected]

diff --git a/index.js b/index.js
index 86e5add10bf78569c4e886c7ed3f8554acd80109..fb1d6f6034eaa5a207118a6088b11046fd2dabaa 100644
--- a/index.js
+++ b/index.js
@@ -23,7 +23,7 @@ async function conventionalChangelogWriterInit (context, options) {
   context = {
     commit: 'commits',
     issue: 'issues',
-    date: dateFormatter.format(new Date()),
+    date: new Date().toISOString().slice(0, 10),
     ...context
   }

peterhirn avatar Feb 27 '24 16:02 peterhirn

Hi @sac-cas I did not unfortunately, but I agree that a dedicated setting would be a nice addition.

bilelomrani1 avatar Feb 28 '24 10:02 bilelomrani1