×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang Date & Time | Find Output Programs | Set 2

This section contains the Golang Date & Time find output programs (set 2) with their output and explanations.
Submitted by Nidhi, on October 22, 2021

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)
}

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.


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))
}

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.


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)
}

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
*/

Golang Find Output Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.