Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse date/time with dots/periods golang

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading