Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Time

This Go code demonstrates working with the time package to handle dates and times. Let's go through the code with inline comments and explanations:

// Importing necessary packages.
import (
	"fmt"
	"time"
)

// The main function, where the execution of the program begins.
func main() {
	// Creating an alias for fmt.Println for brevity.
	p := fmt.Println

	// Getting the current time.
	now := time.Now()
	p(now)

	// Creating a specific date and time.
	then := time.Date(
		2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
	p(then)

	// Extracting various components of the time.
	p(then.Year())
	p(then.Month())
	p(then.Day())
	p(then.Hour())
	p(then.Minute())
	p(then.Second())
	p(then.Nanosecond())
	p(then.Location())

	// Determining the weekday.
	p(then.Weekday())

	// Comparing two times.
	p(then.Before(now))
	p(then.After(now))
	p(then.Equal(now))

	// Calculating the time difference.
	diff := now.Sub(then)
	p(diff)

	// Extracting components of the time difference.
	p(diff.Hours())
	p(diff.Minutes())
	p(diff.Seconds())
	p(diff.Nanoseconds())

	// Adding and subtracting time durations.
	p(then.Add(diff))
	p(then.Add(-diff))
}

Output

2024-01-24 13:15:46.5888334 +0530 IST m=+0.001584301
2009-11-17 20:34:58.651387237 +0000 UTC
2009
November
17
20
34
58
651387237
UTC
Tuesday
true
false
false
124331h10m47.937446163s
124331.17998262393
7.459870798957436e+06
4.475922479374462e+08
447592247937446163
2024-01-24 07:45:46.5888334 +0000 UTC
1995-09-12 09:24:10.713941074 +0000 UTC

Explanation:

  1. Getting Current Time:

    • time.Now() returns the current time.

  2. Creating Specific Time:

    • time.Date() is used to create a specific date and time.

  3. Extracting Components:

    • Various methods (Year(), Month(), Day(), etc.) are used to extract components of the time.

  4. Weekday:

    • Weekday() returns the day of the week.

  5. Comparing Times:

    • Before(), After(), and Equal() methods are used to compare times.

  6. Time Difference:

    • Sub() calculates the time difference between two times.

  7. Extracting Components of Time Difference:

    • Methods like Hours(), Minutes(), Seconds(), and Nanoseconds() are used to extract components of the time difference.

  8. Adding and Subtracting Time Durations:

    • Add() is used to add or subtract a time duration.

This code provides a basic overview of working with dates and times in Go using the time package. It covers creating, manipulating, comparing times, and calculating time differences.

PreviousXMLNextEpoch

Last updated 1 year ago

Was this helpful?