Golang, get the difference between two times in minutes

Hi Friends 👋,

Welcome To aGuideHub! ❤️

To get the difference between two times in minutes,

  1. subtract times using the time Sub() method.
  2. Use the Minutes() method to get difference time in minutes.

Today, I’m going to show you how do you get the difference between two times in minutes in golang, here we will use the golang time.Sub() method to subtract time, and time.Minutes() to get the difference in minutes.

Let’s start today’s tutorial How do I get difference between two times in minutes in golang?

Here, we are going to do,

  1. Define two date times object.
  2. Use Sub() to get difference time.
  3. Use the Minutes() method to get the difference in minutes.
package main

import (
    "fmt"
    "time"
)

func main() {
    currentTime := time.Now()
    oldTime := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
    diff := currentTime.Sub(oldTime)

    //In minutes
    fmt.Printf("Minutes: %f\n", diff.Minutes())
}

The above program will print a number of minutes between two dates in the golang console, let’s check the output.

diff minutes, golang

Try it yourself

All the best 👍

Follow me on Twitter