Golang program to add two dates


In this Golang article, we will write programs to add two dates. The dates are added using methods like Add(), AddDate(). There are different formats to represent a date. Here, the dates after addition will be represented using YY/MM/DD format.

Syntax

func (t Time) Sub(u Time) Duration

The sub() function in Go is used to get the difference between two dates. In this function the first two parameters i.e., t and u are date values and this function returns the difference between two values in hours, minutes and seconds.

time.Parse()

This function belongs to the time package. It takes two inputs the layout and the value, layout signifies the format which the string will follow and the value signifies the string which is to be formatted.

time.AddDate()

This function belongs to the time package. It returns a value that is obtained by add no. of years, months and days to the value obtained by time.Date.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and the time package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a main function and in that function create two dates with name date1 and date2 that are to be added together.

  • Step 3 − Use time.Date function to create two dates with input values as the year, month, day, hour, minute, second and time zone information.

  • Step 4 − Then, add the dates together by using AddDate method of date1 to add the day, month and year of date2 to date1.

  • Step 5 − Here, we use Day method to get the day of the month and YearDay method to get the day of the year.

  • Step 6 − Then, 1 is subtracted as the day of the year is 0 indexed here according to the AddDate method.

  • Step 7 − The Output will be the added dates printed on the console using Println function from fmt package.

Example 1

In this example, we will write a Golang program to add two dates using AddDate() method. We will create two dates date1 and date2, then add these dates using the AddDate function and store the data in the sum_of_dates.

package main

import (
   "fmt"
   "time"
)

func main() {
   date1 := time.Date(2023, time.February, 20, 0, 0, 0, 0, time.UTC) 
   date2 := time.Date(2023, time.February, 21, 0, 0, 0, 0, time.UTC) 
   sum_of_dates := date1.AddDate(0, 0, date2.Day()+date2.YearDay()-1) 
   fmt.Println("The dates are added as follows:")
   fmt.Println(sum_of_dates) 
}

Output

The dates are added as follows:
2023-05-03 00:00:00 +0000 UTC

Example 2

In this example, we will write a Golang program to add two dates using Add() function . We will create two date strings date1 and date2 which will be added together using Add and Sub method, and the format will be similar to as given in the layout.

package main

import (
   "fmt"
   "time"
)


func main() {
   date1 := "2022-02-20"  
   date2 := "2023-02-21"  
   layout := "2006-01-02" 
   date1val, _ := time.Parse(layout, date1)
   date2val, _ := time.Parse(layout, date2)
   sum_of_dates := date1val.Add(date2val.Sub(date2val))  
   fmt.Println("The sum of the dates is done as:")
   fmt.Println(sum_of_dates.Format(layout))  
}

Output

The sum of the dates is done as:
2022-02-20

Example 3

In this example, we will write a Golang program to add two dates using the Parse and AddDate method. The string will be parsed to date and then added together using AddDate method.

package main

import (
   "fmt"
   "time"
)

func main() {
   date1 := "2023-03-24"
   date2 := "2023-03-26"

   dt1, err1 := time.Parse("2006-01-02", date1)
   dt2, err2 := time.Parse("2006-01-02", date2)

	
   if err1 != nil {
      panic(err1)
   }
   if err2 != nil {
      panic(err2)
   }

	
   result := dt1.AddDate(0, 0, int(dt2.Sub(dt1).Hours()/24))

   result_str := result.Format("2006-01-02")

	
   fmt.Println("The addition of two dates is:")
   fmt.Println(result_str)
}

Output

The addition of two dates is:
2023-03-26

Conclusion

We executed and concluded the program of adding two dates using three examples. In the first example we used AddDate method to calculate the sum of two dates and in the second example we used sub method to calculate the duration and add two dates using Add method and in the third example we used Parse and AddDate method together.

Updated on: 03-Apr-2023

267 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements