Home > Back-end >  How to parse date/time with dots/periods golang
How to parse date/time with dots/periods golang

Time:09-04

How do I parse date/time string with dots/periods 01.08.2022 17:00:02

package main

import "fmt"
import "time"

func main() {
    date, err := time.Parse("2.Jan.2006 15:04:05", "01.08.2022 17:00:02")
    if err != nil {
        panic(err)
    }

    fmt.Println(date)
}

This results in panic: parsing time "01.08.2022 17:00:02" as "2.Jan.2006 15:04:05": cannot parse "08.2022 17:00:02" as "Jan"

CodePudding user response:

The correct format is:

date, err := time.Parse("2.1.2006 15:04:05", "01.08.2022 17:00:02")

The Jan in format will require the month name spelled out instead of a number.

  • Related