
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - Parse Date Strings
Parsing Strings into Time in Golang
In Golang, parsing string involves converting a string representation into a specific format as date into a time.Time object. This allows you to perform various date and time operations, such as formatting, arithmetic, and comparisons.
Using the Parse Function
Here, we can use the Parse function that is provided by the time package, and we don't use codes like most other languages to represent the component parts of a date/time string. Instead, Go uses the mnemonic device - standard time as a reference.
For example, the reference time can either look like this −
Mon Jan 2 14:10:05 MST 2020 (MST is GMT-0700)
Or, it can look like this as well.
01/02 03:04:10PM '20 -0700
Syntax of Parse() Function
The syntax of the Parse() function is shown below.
func Parse(layout, value string) (Time, error)
The Parse function takes a layout and a value as the arguments and it returns the time and error. The layout is used as a reference and the value is the actual date string that we want to parse.
Example 1: Parsing a Custom Date-Time Format
In this example, we parses the string 'v' representing a date-time according to the layout 'l' into a 'time.Time' object 'tt'. The parsed date-time value is printed. Errors during parsing are ignored.
package main import ( "fmt" "time" ) func main() { v := "Thu, 05/19/11, 10:47PM" l := "Mon, 01/02/06, 03:04PM" tt, _ := time.Parse(l, v) fmt.Println(tt) }
Output
If we run the above code with the command 2014-11-12 11:45:26.371 +0000 UTC, then we will get the following output.
2011-05-19 22:47:00 +0000 UTC
Instead of passing a layout of our own, we can also pass a format that the Go time package provide us and it will parse the date as well.
Example 2: Parsing an RFC3339 Date-Time String
In this example, we parses the string 'str' representing a date-time in RFC3339 format into a time.Time object 'tt'. If there is an error during parsing, it prints the error; otherwise, it prints the parsed date-time value.
package main import ( "fmt" "time" ) func main() { str := "2014-11-12T11:45:26.371Z" tt, err := time.Parse(time.RFC3339, str) if err != nil { fmt.Println(err) } fmt.Println(tt) }
Output
If we run the above code with the command go run main.go, then we will get the following output.
2014-11-12 11:45:26.371 +0000 UTC