Go offers extensive support for times and durations; here are some examples.
packagemainimport "fmt"
import "time"
funcmain() {
p := fmt.Println
// We’ll start by getting the current time.
now := time.Now()
p(now)
// You can build a time struct by providing the year, month, day, etc. Times are always associated with a Location, i.e. time zone.
then := time.Date(
2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
p(then)
// You can extract the various components of the time value as expected.
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())
// The Monday-Sunday Weekday is also available.
p(then.Weekday())
// These methods compare two times, testing if the first occurs before, after, or at the same time as the second, respectively.
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))
// The Sub methods returns a Duration representing the interval between two times.
diff := now.Sub(then)
p(diff)
// We can compute the length of the duration in various units.
p(diff.Hours())
p(diff.Minutes())
p(diff.Seconds())
p(diff.Nanoseconds())
// You can use Add to advance a time by a given duration, or with a - to move backwards by a duration.
p(then.Add(diff))
p(then.Add(-diff))
}
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Output for the above go program :
$ go run time.go
2012-10-3115:50:13.793654 +0000 UTC
2009-11-1720:34:58.651387237 +0000 UTC
2009
November
17203458651387237
UTC
Tuesday
truefalsefalse25891h15m15.142266763s
25891.254206185211.5534752523711128e+069.320851514226677e+07932085151422667632012-10-3115:50:13.793654 +0000 UTC
2006-12-0501:19:43.509120474 +0000 UTC
Related Searches to Golang Time | Time Functions In Golang
golang time sincegolang time subtractgolang timezonegolang time.since millisecondsgolang time.duration examplegolang stopwatchgolang time since secondsgolang duration to millisecondsgolanggoprogoogle gogolang tutorialgoogle languagego languagego programming languagego languagego programming languagegolang tutorialgo googlegoogle gogolanggp languagego programming language bookgolang coursethe go programming languagebest book to learn golanggolang web development tutorialgolang big datago language bookbig data golanggolang booklearn golanggolang programsgo programming language usesgo to google go to googlelearn go programminggoogle and gowhat is go programming languagego google go google go googlewhich programming language used in googlego to the bookgo scripting languagegolang online tutorialgoogle language programminggo language programminggolang iotgo to go to googlewhy go languagego programming language tutorialgo language tutoriallearn go languagegolang jobsgoogle go softwareprogramming language gogo programing languagegolang web frameworkgo for googlego language ide for windowsgoogle golanggolang programminggoogle coding languagegolang projectsgo programming tutorialgoogle go language tutorialgolang debuggergo to the googlegoogle go programming languagegolang classgolang serverprogramming language used by googlegoogle go languagego programminggo programming pdfgolang developmentgolang frameworkgo language examplesgolang tutorial pdflanguage googlego programgo programming language pdf