Convert UTC string to time object

golang convert string to time object
convert string to time python
golang convert time to utc
golang time
cast time to string golang
golang datetime string to time
golang time format
parsing time to string golang

I have this datetime, or something that looks like it.

2014-11-17 23:02:03 +0000 UTC

I want to convert this to a time object and I've been unable to produce any output from time.Parse apart from:

0001-01-01 00:00:00 +0000 UTC

I've tried these layouts:

time.RFC3339
0001-01-01 00:00:00 0000 UTC
2016-10-10
time.UnixDate

And a few more - none have worked.

This is how I'm calling parse :

updatedAt, err := time.Parse(time.UnixDate, updatedAtVar)

How do I create a time object from a string?

Most likely you used a wrong layout, and you didn't check the returned error.

The layout must be this date/time, in the format your input time is:

Mon Jan 2 15:04:05 -0700 MST 2006

See this working code:

layout := "2006-01-02 15:04:05 -0700 MST"
t, err := time.Parse(layout, "2014-11-17 23:02:03 +0000 UTC")
fmt.Println(t, err)

Output (try it on the Go Playground):

2014-11-17 23:02:03 +0000 UTC <nil>

EDIT:

In your question you included a + sign in your input time (as part of the zone offset), but you have error with times of other formats.

Time.String() uses the following format string:

"2006-01-02 15:04:05.999999999 -0700 MST"

So either use this to parse the times, or use Time.Format() to produce your string representations where you can specify the layout, so you can use the same layout to parse the time strings.

2nd round:

You're including your time strings in URLs. The + sign is a special character in URL encoding: it denotes the space. So the + gets converted to space (and so it vanishes from your time string). Use proper URL encoding! Check out the net/url package, and this example.

Converting Strings to datetime in Python, If our input string to create a datetime object is in the same ISO 8601 format, we can Now, let's use the pytz library to convert the above timestamp to UTC. Parse the string of time to time object. Date d = dateFormat.parse (strTime); The following is the complete example.

Didn't see this yet but for those that don't know the formats, time has the formats builtin as constants. so you can reference them when parsing or formating.

time.Parse(time.RFC3339, <your time.Time object here>)
<time.Time object>.Format(time.RFC3339) //or other type of formats

Here they are for reference

ANSIC       = "Mon Jan _2 15:04:05 2006"
UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
RFC822      = "02 Jan 06 15:04 MST"
RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339     = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen     = "3:04PM"

Date.parse(), const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT'); for example in conjunction with the setTime() method and the Date object. (See the section Date Time String Format in the ECMAScript specification for more details.) values depending on the format of the string that is being converted. Java program to convert string to OffsetDateTime and get equivalent instant in UTC. ‘Z’ in date string represents the UTC timezone. It is short form of Zulu and can be written as UTC +0:00. Parse String to OffsetDateTime

You are likely using the wrong layout. As explained in time.Parse, you need to specify a layout that helps Go to understand how the date passed as input is formatted.

There are predefined layouts (like the ones you were using), but none matches your input. Hence you need to define a custom layout.

A layout uses the following date as reference:

Mon Jan 2 15:04:05 MST 2006

The layout is nothing else that a representation of that date, that matches the representation of your input:

t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", "2014-11-17 23:02:03 +0000 UTC")

Also remember to check err for errors. It's likely your attempts returned an error, but you didn't check it.

package main

import (
    "fmt"
    "log"
    "time"
)

func main() {
    t, err := time.Parse("2006-01-02 15:04:05 -0700 UTC", "2014-11-17 23:02:03 +0000 UTC")
    if err != nil {
        log.Fatalln(err)
    }
    fmt.Println(t)
}

Convert UTC string to golang time object - datetime - html, I have this datetime, or something that looks like it. 2014-11-17 23:02:03 +0000 UTC I want to convert this to a time object and I've been unable to produce any  Inside of this strptime () function, we specify the string object that we want to convert as the first parameter and the format of the date that the string object is in. We set this value equal to a variable; this variable is then the converted datetime object of the string object.

Arrow: Better dates & times for Python, Some ISO 8601 compliant strings are recognized and parsed without a format string: Convert¶. Convert from UTC to other timezones by name or tzinfo: >>> utc Returns a datetime object, converted to the specified timezone. Parameters. +00:00 is the difference between the displayed time and the UTC time. In this example the value of tzinfo happens to be UTC as well, hence the 00:00 offset. In this case, the datetime object is a timezone-aware object. Similarly, we can convert date-time strings to any other timezone.

DateTime.ParseExact Method (System), Converts the specified string representation of a date and time to its equivalent. The format of the string An object that supplies culture-specific format information about s . dateString); } // Parse a string representing UTC. dateString  In dart, we can easily convert a string to a DateTime object. DateTime.parse() method takes one correctly formatted string as input and convert it to a DateTime object. Similarly, we can convert a DateTime object to a ISO8601 string using toIso8601String() method.

Convert String to Date in Java, The Date-Time API provide parse() methods for parsing a String that contains date and time information. To convert String objects to LocalDate  Converting a datetime object to UTC is a common and important part of modern software development. Server time and dates stored in databases are often in UTC, so converting a date to UTC is something that not only python developers, but pretty much any developer will find themselves doing time and time again.

Comments
  • lol..yes. I used the wrong layout. I have no idea what layout to use. I just tried "2006-01-02 15:04:05 -0700 MST" and it didn't work either. Was wondering if someone knew what layout to use because this is a common format. I'm literally going time.Now(), converting it to String, and passing it in to unpack (writing an integration test)
  • @praks5432 What do you mean? It's not working for you? Click on the Playground link in my answer and try it yourself.
  • I pasted in 2014-11-17 23:02:03 0000 UTC which is the output of time.Now().String() (I froze time - for a test) I get this error in your playground 0001-01-01 00:00:00 +0000 UTC parsing time "2014-11-17 23:02:03 0000 UTC" as "2006-01-02 15:04:05 -0700 MST": cannot parse "UTC" as "-0700"
  • @praks5432 You have a missing + sign in your input, as part of the zone offset.
  • Yes. I know. I don't control that. I'm passing in time.Now().String(). That's from Go. How can we have a function in one package that won't take the output of another function in the same package that seem to be designed to work together?
  • Super reference - thanks!
  • Yes, I understand. But what I'm doing is using time.Now().String as an url parameter. I'm then unpacking that parameter and trying to convert back to a time (this is a test that I'm writing). The output I get from time.Now().String once it is unpacked is 2014-11-17 23:02:03 0000 UTC, which is not being parsed. I know I have the wrong layout. I'm trying to find which layout.