Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7062767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:38:01+00:00 2026-05-28T04:38:01+00:00

I print the start and end time using date +"%T" , which results in

  • 0

I print the start and end time using date +"%T", which results in something like:

10:33:56
10:36:10

How could I calculate and print the difference between these two?

I would like to get something like:

2m 14s
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:38:01+00:00Added an answer on May 28, 2026 at 4:38 am

    Bash has a handy SECONDS builtin variable that tracks the number of seconds that have passed since the shell was started. This variable retains its properties when assigned to, and the value returned after the assignment is the number of seconds since the assignment plus the assigned value.

    Thus, you can just set SECONDS to 0 before starting the timed event, simply read SECONDS after the event, and do the time arithmetic before displaying.

    #!/usr/bin/env bash
    
    SECONDS=0
    # do some work
    duration=$SECONDS
    echo "$((duration / 60)) minutes and $((duration % 60)) seconds elapsed."
    

    As this solution doesn’t depend on date +%s (which is a GNU extension), it’s portable to all systems supported by Bash.

    Documentation:

    SECONDS This variable expands to the number of seconds since the shell
    was started. Assignment to this variable resets the count to the value
    assigned, and the expanded value becomes the value assigned plus the
    number of seconds since the assignment. The number of seconds at shell
    invocation and the current time are always determined by querying the
    system clock. If SECONDS is unset, it loses its special properties,
    even if it is subsequently reset. [https://www.gnu.org/software/bash/manual/bash.html]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Flex 3 how to calculate start and end angle(point) of every pie chart
I am using Google's Annotated Time Line tool to plot data which spans over
Recently I was trying to determine the time needed to calculate a waveform using
The print functionality of Excel (using VBA) is extremely slow. I'm hoping someone has
I am using the following way to cout a function's time: #define TIME_COST(message, ...)\
I'm trying to benchmark the difference between a function pointer call and a virtual
I am trying to write a class that will calculate checksums using multiple processes,
In using vim, when I start a comment with //, immediately after I type
I'm Trying to read from a socket and print to stdout using printf (a
Okay, so I have a start datetime and an end datetime for multiple items.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.