In this section, we are going to learn how to find the difference between two dates. Let's imagine that we have two dates, as shown in the following code block, and you will see that the signature of this method is self-explanatory. So, all we have to do is use the following code for the subtract method which subtracts the first date from the second one:
package main import ( "time" "fmt" ) func main(){ first := time.Date(2017, 1,1,0,0,0,0,time.UTC) second := time.Date(2018, 1,1,0,0,0,0,time.UTC) difference := second.Sub(first) fmt.Printf("Difference %v", difference) }
Now, before we run the code and obtain the output, if you check the signature, you will see that the method returns Duration
instead of the Time
type between the dates:
Getting back to running our code, you'll see the following output:
This is how you simply find the difference between two dates. In our next section, we're going to learn how to parse dates and times from...