Programming Language: Golang

Namespace/Package Name: Time

Class/Type: Time

Method/Function: Add

Examples at hotexamples.com: 30

The `time` package in Go provides functionality to work with dates and times. One of the functions in this package is `Add`, which allows adding a duration to a given time. Here are some examples of using this function:

**example 1**, we add one hour to the current time using the `Add` function and the `time.Hour` constant. This output the time one hour from now.
In **example 2**, we add 10 minutes and 30 seconds to a specific time using the `Add` function and arithmetic with `time.Minute` and `time.Second` constants. This outputs the time 10 minutes and 30 seconds later.
In **example 3**, we add a negative duration (i.e., subtract) one hour from the current time using the `Add` function and the negation of `time.Hour`. This outputs the time one hour ago.
We can determine that the package library for this function is the `time` package in Go.

package main import ( "fmt" "time" ) func main() { //InExample 1:Add one hour to the current time now := time.Now() oneHourLater := now.Add(time.Hour) fmt.Println("One hour from now:", oneHourLater) //Example 2:Add 10 minutes and 30 seconds to a specific time someTime := time.Date(2021, 1, 1, 12, 0, 0, 0, time.UTC) newTime := someTime.Add(time.Minute * 10 + time.Second * 30) fmt.Println("10 minutes and 30 seconds later:", newTime) //Example 3:Add a negative duration to a time timeInThePast := time.Now().Add(-time.Hour) fmt.Println("One hour ago:", timeInThePast) }

Frequently Used Methods

Frequently Used Methods

Frequently Used Methods

Related in langs

Frequently Used Methods

Related