Q:

Golang Date & Time | Find Output Programs | Set 2

0

This section contains the Golang Date & Time find output programs (set 2) with their output and explanations.

Program 1:

package main

import "fmt"
import "time"

func main() {
	date := time.Date(0, 0, 0, 10, 10, 10, 0, time.UTC)
	res := date.AddTime(10, 5, 7)

	fmt.Println("Res : ", res)
}

Program 2:

package main

import "fmt"
import "time"

func main() {
	date1 := time.Date(2022, 2, 5, 0, 0, 0, 0, time.UTC)
	date2 := time.Date(2021, 1, 14, 0, 0, 0, 0, time.UTC)

	fmt.Println("Result: ", date1.After(date2))
}

Program 3:

package main

import "fmt"
import "time"

func main() {
	time1 := time.Date(2020, 2, 5, 10, 5, 20, 0, time.UTC)
	time2 := time.Date(2020, 2, 5, 13, 10, 30, 0, time.UTC)

	diff := time2.Subtract(time1)
	fmt.Printf("Difference of t1 and t2 is: %v", diff)
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

./prog.go:8:13: date.AddTime undefined (type time.Time has no field or method AddTime)

Explanation:

The above program will generate a syntax error because AddTime() is not defined in the Time class.


 

Answer Program 2:

Output:

Result:  true

Explanation:

In the above program, we created two date objects date1 and date2. Here, we used After() method, which is used to compare two dates and return a Boolean value.


 

Answer Program 3:

Output:

./prog.go:10:15: time2.Subtract undefined (type time.Time has no field or method Subtract)

Explanation:

The above program will generate a syntax error because Subtract() is not a built-in method. The Sub() method is used to subtract a date from another date.

The correct program is given below:

package main

import "fmt"
import "time"

func main() {
	time1 := time.Date(2020, 2, 5, 10, 5, 20, 0, time.UTC)
	time2 := time.Date(2020, 2, 5, 13, 10, 30, 0, time.UTC)

	diff := time2.Sub(time1)
	fmt.Printf("Difference of t1 and t2 is: %v", diff)
}

/*
Output:
Difference of t1 and t2 is: 3h5m10s
*/

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now