Golang Time parsing question. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Golang Time parsing question.

I have a time string in format "hour:minute: seconds: milliseconds" , ie "11:08:27:18". I need to parse this and get back a string after adding 30 seconds. So the output should be "11:08:57:18". Pls help

3rd Oct 2022, 7:24 AM
Good coder
1 Answer
0
package main import ( "fmt" "time" "strings" ) func main() { // Input time string in "hour:minute:seconds:milliseconds" format inputTimeStr := "11:08:27:18" // Parse the input time string layout := "15:04:05:00" // Use the time layout that matches your input format parsedTime, err := time.Parse(layout, inputTimeStr) if err != nil { fmt.Println("Error parsing time:", err) return } // Add 30 seconds to the parsed time modifiedTime := parsedTime.Add(30 * time.Second) // Format the modified time back into the desired format resultTimeStr := modifiedTime.Format(layout) fmt.Println("Original time:", inputTimeStr) fmt.Println("Modified time:", resultTimeStr) }
30th Sep 2023, 7:02 PM
D1M3
D1M3 - avatar