MOCKSTACKS
EN
Questions And Answers

More Tutorials








Go Date Time

Go supports time formatting and parsing via pattern-based layouts. To format time, we use the Format() function which formats a Time object.

FormatExample
ANSIC“Mon Jan _2 15:04:05 2006”
UnixDate“Mon Jan _2 15:04:05 MST 2006”
RubyDate“Mon Jan 02 15:04:05 -0700 2006”
RFC822“02 Jan 06 15:04 MST”
RFC822Z“02 Jan 06 15:04 -0700”
RFC850“Monday, 02-Jan-06 15:04:05 MST”
RFC1123“Mon, 02 Jan 2006 15:04:05 MST”
RFC1123Z“Mon, 02 Jan 2006 15:04:05 -0700”
RFC3339“2006-01-02T15:04:05Z07:00”
RFC3339Nano“2006-01-02T15:04:05.999999999Z07:00”


Example of Date Time in Go

package main
  
import (
    "fmt"
    "time"
)
  
func main() {
  
    current_time := time.Now()
  
    fmt.Println("ANSIC: ", current_time.Format(time.ANSIC))
    
    fmt.Println("UnixDate: ", current_time.Format(time.UnixDate))
    
    fmt.Println("RFC1123: ", current_time.Format(time.RFC1123))
    
    fmt.Println("RFC3339Nano: ", current_time.Format(time.RFC3339Nano))
    
    fmt.Println("RubyDate: ", current_time.Format(time.RubyDate))
}

Output

ANSIC: Fri Jan 21 05:41:14 2022
UnixDate: Fri Jan 21 05:41:14 UTC 2022
RFC1123: Fri, 21 Jan 2022 05:41:14 UTC
RFC3339Nano: 2022-01-21T05:41:14.4431657Z
RubyDate: Fri Jan 21 05:41:14 +0000 2022



String To Date Time in Go

In Go we can typecast from string to time using the function time.Parse() from package time.

package main
import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("Time parsing");
    dateString := "2014-11-12T11:45:26.371Z"
    time1, err := time.Parse(time.RFC3339,dateString);
    if err!=nil {
     fmt.Println("Error while parsing date :", err);
    }
    fmt.Println(time1); 
}

Output

Time parsing
2014-11-12 11:45:26.371 +0000 UTC


Another example of String To Date Time in Go

In Go we can typecast from string to time using the function time.Parse() from package time.

package main
import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("Time parsing");
    dateString := "2014-11-12T11:45:26.371Z"
    time1, err := time.Parse(time.RFC3339Nano,dateString);
    if err!=nil {
     fmt.Println("Error while parsing date :", err);
    }
    fmt.Println(time1); 
}

Output

Time parsing
2014-11-12 11:45:26.371 +0000 UTC

Conclusion

In this page (written and validated by ) you learned about Go Date Time . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Thread - Goroutines.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.