time.Time.String() Function in Golang With Examples

In Go language, time packages supplies functionality for determining as well as viewing time. The Time.String() function in Go language is used to yield the time which is formatted with the help of the format string. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.

Syntax:

func (t Time) String() string

Here, “t” is the stated time.

Note: It the stated time has a clock reading that is monotonic then the string that is returned as an output includes a final field i.e, “m=±value”. Where value is the unvaried clock reading that is arranged as a decimal number of seconds.

Return Value: It returns the time which is formatted with the help of format string.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// Time.String() function
  
// Including main package
package main
  
// Importing fmt and time
import (
    "fmt"
    "time"
)
  
// Calling main
func main() {
  
    // Defining the time for String method
    Time := time.Date(2020, 11, 14, 10, 45, 16, 0, time.UTC)
  
    // Calling String method
    t := Time.String()
  
    // Prints output
    fmt.Printf("The time without nanoseconds is: %v\n", t)
}

chevron_right


Output:

The time without nanoseconds is: 2020-11-14 10:45:16 +0000 UTC

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// Time.String() function
  
// Including main package
package main
  
// Importing fmt and time
import (
    "fmt"
    "time"
)
  
// Calling main
func main() {
  
    // Defining the time for String method
    Time := time.Date(2020, 11, 14, 36, 45, 16, 36, time.UTC)
  
    // Calling String method
    t := Time.String()
  
    // Prints output
    fmt.Printf("The time with nanoseconds is: %v\n", t)
}

chevron_right


Output:

The time with nanoseconds is: 2020-11-15 12:45:16.000000036 +0000 UTC

Here, the hour stated in the above code is out of usual range but it is normalized, while conversion and nanoseconds are also included in the time, stated above.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.