Golang Time.AppendFormat() function is “used to append the stated textual representation to the stated ‘b’.”
Syntax
func (t Time) AppendFormat(b []byte, layout string) []byte
Parameters
- t: It is the stated time.
- b: It is the byte of the slice, and the layout holds a string type.
Return value
It appends the textual representation to the “b” and returns the extended buffer.
Example 1: How to Use Time.AppendFormat() Function
package main
import (
"fmt"
"time"
)
func main() {
// Define a specific date
date := time.Date(2023, 7, 1, 12, 0, 0, 0, time.UTC)
// Create a slice to append to
dest1 := []byte("Date in YYYY-MM-DD format: ")
dest2 := []byte("Date in DD/MM/YYYY format: ")
dest3 := []byte("Date in MM-DD-YYYY format: ")
// Format the date and append it to the slice
dest1 = date.AppendFormat(dest1, "2006-01-02")
dest2 = date.AppendFormat(dest2, "02/01/2006")
dest3 = date.AppendFormat(dest3, "01-02-2006")
fmt.Println(string(dest1))
fmt.Println(string(dest2))
fmt.Println(string(dest3))
}
Output
Date in YYYY-MM-DD format: 2023-07-01
Date in DD/MM/YYYY format: 01/07/2023
Date in MM-DD-YYYY format: 07-01-2023
Example 2: Format the time and append it
package main
import (
"fmt"
"time"
)
func main() {
// Get the current time
now := time.Now()
// Create a slice to append to
dest := []byte("Current time: ")
// Format the time and append it to the slice
dest = now.AppendFormat(dest, "2006-01-02 15:04:05")
fmt.Println(string(dest))
}
Output
Current time: 2023-07-01 21:37:43
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.