Fetch GitHub Commit Date (JS/TS)

How to fetch the date of a commit from the GitHub API

Snippets ->

1 minute read

For an entire repository:

const response = await fetch(
  'https://api.github.com/repos/[user]/[repo]/commits'
)
const commits = await response.json()
const latestCommit = commits[0]
const latestCommitDate = latestCommit.commit.author.date

For a specific file within a repository:

const response = await fetch(
  'https://api.github.com/repos/[user]/[repo]/commits?path=[pathToFile]'
)
const commits = await response.json()
const latestCommit = commits[0]
const latestCommitDate = latestCommit.commit.author.date

Last Updated: 1650634438000

Edit on GitHub ->