SoFunction
Updated on 2025-03-05

Operations of printing specific dates and times using Golang

Basic time operation

First, let’s take a look at some basic time operations.

Get the current time to use()function, it returns the current time object, type. Here is an example:

package main
import (
    "fmt"
    "time"
)
func main() {
    currentTime := ()
    ("Current time is", currentTime)
}

The output result is similar to:

Current time is 2022-05-24 11:07:36.710239 +0800 CST m=+0.000149139

The format here is the default. If we want to output time in a specific format, we need to use()function

package main
import (
    "fmt"
    "time"
)
func main() {
    currentTime := ()
    ("Current time is", ("2006-01-02 15:04:05"))
}

The output result is similar to:

Current time is 2022-05-24 11:08:11

A special date format string is used here"2006-01-02 15:04:05", its meaning is:

  • 2006 indicates year
  • 01 indicates month
  • 02 indicates date
  • 15 means hours
  • 04 means minutes
  • 05 means seconds

It should be noted that the numbers in the format string must be these specific numbers, otherwise an error will occur.

We can also use()Function converts a string intoObject.

package main
import (
    "fmt"
    "time"
)
func main() {
    timeStr := "2022-05-24 11:08:11"
    parsedTime, _ := ("2006-01-02 15:04:05", timeStr)
    ("Parsed time is", parsedTime)
}

The output result is similar to:

Parsed time is 2023-05-24 11:08:11 +0000 UTC

Specific date and time format

In the example above, we use a specific date format string. Here are some commonly used date and time formats:

  • 2006-01-02: Date, such as 2023-05-24
  • 15:04:05: Time, such as 11:08:11
  • 2006-01-02 15:04:05: Date and time, such as 2023-05-24 11:08:11
  • 01/02/06 3:04 PM: US date and time format, such as 05/24/22 11:08 AM
  • 02/01/2006 15:04: European date and time format, such as 24/05/2022 11:08

In addition to the above format, Golang also provides a richer date and time specific formats, please refer to the official documentation for more information.

Custom date and time format

If the specific date and time format provided above does not meet our needs, we can customize the date and time format.

package main
import (
    "fmt"
    "time"
)
func main() {
    currentTime := ()
    customFormat := "January 2, 2006 15:04:05"
    ("Current time is", (customFormat))
}

The output result is similar to

Current time is May 24, 2023 11:14:53

Parsing date and time strings in different formats

Sometimes we encounter various date and time string formats, and we need to be able to parse them correctly

package main
import (
    "fmt"
    "time"
)
func main() {
    timeStr := "2023-05-24 11:08:11"
    parsedTime, _ := ("2006-01-02 15:04:05", timeStr)
    ("Parsed time is", parsedTime)
    timeStr2 := "05/24/22 11:08 AM"
    parsedTime2, _ := ("01/02/06 3:04 PM", timeStr2)
    ("Parsed time is", parsedTime2)
    timeStr3 := "May 24, 2023 11:14:53"
    parsedTime3, _ := ("January 2, 2006 15:04:05", timeStr3)
    ("Parsed time is", parsedTime3)
}

Get the specified date and time

Sometimes we need to get the specified date and time, which can be used()function.

package main
import (
    "fmt"
    "time"
)
func main() {
    specTime := (2023, 5, 24, 12, 0, 0, 0, )
    ("Specified time is", ("2006-01-02 15:04:05"))
}

The output result is similar to:

Specified time is 2022-05-24 12:00:00

This is the article about using Golang to print specific dates and time. For more information about Golang to print specific dates and time, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!