How to parse datetime that ends with `Z`?

z at the end of datetime
datetime isoformat parse
python string to datetime with z
python parse datetime with timezone
invalid isoformat string
datetime strptime
datetime-tz
python t in datetime

I have the following datetime string s:

2017-10-18T04:46:53.553472514Z

I parese it like that:

t = datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ')

how to fix ValueError: time data '2017-10-18T04:46:53.553472514Z' does not match format '%Y-%m-%dT%H:%M:%SZ'

In theory,

t = datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%fZ')

would be the correct format string as you have fractions of second as well. BUT they would then need to be microseconds. Yours are probably nanoseconds, as %f only takes maximum of 6 digits.

So you need to do something like this:

t = datetime.datetime.strptime(s.split(".")[0], '%Y-%m-%dT%H:%M:%S')
t = t + datetime.timedelta(microseconds=int(s.split(".")[1][:-1])/1000)

print (t)

This works but it converts nanoseconds to microseconds. If this is not ok, then you need to do something else.

What is this date format? 2011-08-12T20:17:46.384Z, rfcformat() and datetime.fromrfcformat() that implement this RFC. As a consequence, this would also allow us to parse dates ending in Z . Let me know  However there is a simplified standard that is open: RFC-3339.I suggest adding new methods datetime.rfcformat() and datetime.fromrfcformat() that implement this RFC. As a consequence, this would also allow us to parse dates ending in Z.

I think you should use dateutil.parser module

In [23]: s = "2017-10-18T04:46:53.553472514Z"

In [24]: import dateutil.parser as p

In [25]: p.parse(s)
Out[25]: datetime.datetime(2017, 10, 18, 4, 46, 53, 553472, tzinfo=tzutc())

SimpleDateFormat(“Z”) in Java, Bug in code for DateTime.Parse (ignores 'Z')? #6121. Closed. jenstMS opened this issue on Jun 25, 2018 — with docs.microsoft.com · 8 comments. Closed  The letter Z may be used as a suffix to denote a time being in the Zulu Time Zone, such as 08:00Z or 0800Z. This is spoken as "zero eight hundred Zulu". Some places with the same time as Zulu Time Zone. London, England, United Kingdom. Lisbon, Lisbon, Portugal. Dublin, Ireland. Reykjavik, Iceland. Freetown, Sierra Leone. Conakry, Guinea.

datetime is another Python built-in module that is completely superseeded by a better extra module: arrow.

You can just do:

import arrow
dt = arrow.get('2017-10-18T04:46:53.553472514Z').datetime

Your date string being in standardized ISO format, it will be parsed without even giving a format string. The parsed df will be:

datetime.datetime(2017, 10, 18, 4, 46, 53, 553472, tzinfo=tzutc())

Or keep the Arrow object in case you want to keep the extra precision

ISO 8601, For example, let's say you have some code that generates a datetime object, but header of an auto-generated letter or report: >>> z datetime.datetime(2012, 9, If you are parsing a lot of dates in your code and you know the precise format,  Text Formula Help, Time shows a Z at the end I did a formula to concatenate a text field and one datetime filed, however the end result shows life this; departed 2011-02-23 15:46:00Z I think this is the server datetime as this does not math what is in the record

I like the mx.DateTime module.

import mx.DateTime as dt
dt.DateTimeFrom('2017-10-18T04:46:53.553472514Z')

+ ZonedDateTime.parse(text,formatter)); out.println(); out.println("Test 2:"); formatter pattern = "EEE, d MMM yyyy HH:mm:ss Z"; formatter = DateTimeFormatter. x: A character vector of dates to parse. format: A format specification, as described below. If set to "", date times are parsed as ISO8601, dates and times used the date and time formats specified in the locale().

DateTime. parse( date ) processed_title = title. downcase.gsub( /[*a-z]+/, '-' ) "#[​processed_date. strftime('%Y-%m-%d')}-#{processed_title}.md." end If we run  The return value and DateTime.Kind. The DateTime.Parse overloads return a DateTime value whose Kind property includes time zone information. It can indicate that the time is: Coordinated Universal Time (System.DateTimeKind.Utc). The time in the local time zone (System.DateTimeKind.Local).

in pseudocode is the following: DATETIME HOST IDENT COLON MESSAGE following example uses both alexer and a parser to demonstrate the concepts. Arguments x. A character vector of dates to parse. format. A format specification, as described below. If set to "", date times are parsed as ISO8601, dates and times used the date and time formats specified in the locale().

The timezone is always zero UTC offset, as denoted by the suffix "Z". The above example uses parsing of a non–standard string value that  With the examples from Microsoft SQL Server, the DateTime.Parse method will parse the string representations from the database. Tip: When using the valid date formats from MySQL and SQL Server, you can use DateTime.Parse to get the dates from the SQL strings.

Comments
  • You don't have the T and the fraction of the second in your format string.
  • updated my question
  • @KlausD. how to add fraction to '%Y-%m-%dT%H:%M:%SZ'?
  • Just to point out, your datetime string is in ISO8601 format.