Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9057913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:35:13+00:00 2026-06-16T14:35:13+00:00

How do I parse non-standard date/time strings in Go. In example if I wanted

  • 0

How do I parse non-standard date/time strings in Go. In example if I wanted to convert the string 10/15/1983 into a time.Time? The time.Parse() function supposedly allows you to specify a format.

http://play.golang.org/p/v5DbowXt1x

package main

import "fmt"
import "time"

func main() {
    test, err := time.Parse("10/15/1983", "10/15/1983")
    if err != nil {
        panic(err)
    }

    fmt.Println(test)
}

This results in a panic.

panic: parsing time "10/15/1983" as "10/15/1983": cannot parse "" as "0/"

Logically that makes sense because how is it supposed to know which is the day and which is the month.

Other languages have a function similar to the following:

parse("mm/dd/yyyy", "10/15/1983")

I cannot find such a function in the Go docs, is my only choice to regex?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:35:14+00:00Added an answer on June 16, 2026 at 2:35 pm

    There are some key values that the time.Parse is looking for.

    By changing:

    test, err := time.Parse("10/15/1983", "10/15/1983")
    

    to

    test, err := time.Parse("01/02/2006", "10/15/1983")
    

    the parser will recognize it.

    Here’s the modified code on the playground.

    package main
    
    import "fmt"
    import "time"
    
    func main() {
        test, err := time.Parse("01/02/2006", "10/15/1983")
        if err != nil {
            panic(err)
        }
    
        fmt.Println(test)
    }
    

    You can utilize the constants list in the src/pkg/time/format.go file to create your own parse formats.

    const (
        stdLongMonth      = "January"
        stdMonth          = "Jan"
        stdNumMonth       = "1"
        stdZeroMonth      = "01"
        stdLongWeekDay    = "Monday"
        stdWeekDay        = "Mon"
        stdDay            = "2"
        stdUnderDay       = "_2"
        stdZeroDay        = "02"
        stdHour           = "15"
        stdHour12         = "3"
        stdZeroHour12     = "03"
        stdMinute         = "4"
        stdZeroMinute     = "04"
        stdSecond         = "5"
        stdZeroSecond     = "05"
        stdLongYear       = "2006"
        stdYear           = "06"
        stdPM             = "PM"
        stdpm             = "pm"
        stdTZ             = "MST"
        stdISO8601TZ      = "Z0700"  // prints Z for UTC
        stdISO8601ColonTZ = "Z07:00" // prints Z for UTC
        stdNumTZ          = "-0700"  // always numeric
        stdNumShortTZ     = "-07"    // always numeric
        stdNumColonTZ     = "-07:00" // always numeric
    )
    

    So anytime your format specifies a year, it should be done with “06” or “2006”, seconds are specified by “05” or “5” and time zones are specified at “MST”, “Z0700”, “Z07:00”, “-0700”, “-07” or “-07:00”. If you reference the constants list you can likely put together any standard format you’d need to parse.

    For example, if you want to parse the date/time in the Common Log Format, the format Apache uses for its log files, you would do so by passing the following string to time.Parse() as the layout argument.

    "02/Jan/2006:15:04:05 -0700"
    

    “02” denotes the day of the month field, “Jan” denotes the month name field, “2006” denotes the year field, “15” denotes the hour of day field in 24 hour format, “04” denotes the minutes field, “05” denotes the seconds field and “-0700” denotes the time zone field.

    That format would parse the current PST time: 31/Dec/2012:15:32:25 -0800

    So the time.Parse() call would look like this:

    test, err := time.Parse("02/Jan/2006:15:04:05 -0700", "31/Dec/2012:15:32:25 -0800")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file which I'd like to parse into a non-XML (text)
I have to parse a String that can assume hex values or other non-hex
I parse some values from an xml file. There is a @25-12-2010'T'23:40:00 string with
I parse xml file in android. I copypasted example... It works with english words,
I need to parse a string like func1(arg1, arg2); func2(arg3, arg4); . It's not
How to parse non well-formed HTML in android ? I tried to use XOM
I need to parse complex (non fixed length) csv files to Java objects in
Apple reject non-renewing subscription apps which doesn't provide user/password server side function to track
I am attempting to work with a very large dataset that has some non-standard
I have a very large JSON string that I need to parse with in-browser

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.