# How to get start time and endtime for a job or step as Buildkite variable

**URL:** <https://forum.buildkite.community/t/how-to-get-start-time-and-endtime-for-a-job-or-step-as-buildkite-variable/3908>\
**Category:** Features Requests\
**Created:** [August 28, 2024, 6:45pm UTC](https://forum.buildkite.community/t/how-to-get-start-time-and-endtime-for-a-job-or-step-as-buildkite-variable/3908 "2024-08-28T18:45:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ismail](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@ismail](https://forum.buildkite.community/u/ismail)\
**Post date:** [August 28, 2024, 6:45pm UTC](https://forum.buildkite.community/t/how-to-get-start-time-and-endtime-for-a-job-or-step-as-buildkite-variable/3908/1 "2024-08-28T18:45:27Z")

</div>

Hi Team,

I have a requirement where I need to get the start and end time of a job/step and post it via `notify` to slack.

It would be great to have that as a variable that can be directly injected but I am open to other solutions as well, if that is not available.

Thanks and Regards,  
Ismail

---

<div class="post-metadata">

**Author:** ![stephanie.atte](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/stephanie.atte/32/1357_2.png) [@stephanie.atte](https://forum.buildkite.community/u/stephanie.atte)\
**Post date:** [August 28, 2024, 7:34pm UTC](https://forum.buildkite.community/t/how-to-get-start-time-and-endtime-for-a-job-or-step-as-buildkite-variable/3908/2 "2024-08-28T19:34:18Z")

</div>

Hey Ismail,

To achieve this workflow, you can use the [GraphQL API](https://buildkite.com/docs/apis/graphql/cookbooks/jobs#get-the-job-run-time-per-build) to query the job’s start and finish times with the query shown below.

```auto
query GetJobRunTimeByBuild{
  build(slug: "org-slug/pipeline-slug/build-number") {
    jobs(first: 1, step: {key: "insert-key"}) {
      edges {
        node {
          ... on JobTypeCommand {
            startedAt
            finishedAt
          }
        }
      }
    }
  }
}

```

`startedAt:` DateTime when the job started running  
`finishedAt`: DateTime when the job finished

Then extract this information then pass these values into a pipeline step for notifying Slack.

```auto
steps:
 - label: "Send Slack Message"
   command: |
      startedAt="...."
      finishedAt"...."
      cat <<- YAML | buildkite-agent pipeline upload 
         steps:
           - label: "Notify slack about Job"
             command: echo "Notify slack about Job"
             notify:
               - slack:
                   channels:
                     - "#general_test"
                   message: "The Job started at $$startedAt and finished at $$finishedAt"
      YAML

```
