How to convert Python’s .isoformat() string back into datetime object [duplicate]

Python 3.7+ As of Python 3.7 there is a method datetime.fromisoformat() which is exactly the reverse for isoformat(). Older Python If you have older Python, then this is the current best “solution” to this question: pip install python-dateutil Then… import datetime import dateutil def getDateTimeFromISO8601String(s): d = dateutil.parser.parse(s) return d

Convert an RFC 3339 time to a standard Python timestamp

You don’t include an example, but if you don’t have a Z-offset or timezone, and assuming you don’t want durations but just the basic time, then maybe this will suit you: import datetime as dt >>> dt.datetime.strptime(‘1985-04-12T23:20:50.52’, ‘%Y-%m-%dT%H:%M:%S.%f’) datetime.datetime(1985, 4, 12, 23, 20, 50, 520000) The strptime() function was added to the datetime module in … Read more

Generate RFC 3339 timestamp in Python [duplicate]

UPDATE 2021 In Python 3.2 timezone was added to the datetime module allowing you to easily assign a timezone to UTC. >>> import datetime >>> n = datetime.datetime.now(datetime.timezone.utc) >>> n.isoformat() ‘2021-07-13T15:28:51.818095+00:00’ previous answer: Timezones are a pain, which is probably why they chose not to include them in the datetime library. try pytz, it has … Read more

How to convert a timezone aware string to datetime in Python without dateutil?

As of Python 3.7, datetime.datetime.fromisoformat() can handle your format: >>> import datetime >>> datetime.datetime.fromisoformat(‘2012-11-01T04:16:13-04:00′) datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000))) In older Python versions you can’t, not without a whole lot of painstaking manual timezone defining. Python does not include a timezone database, because it would be outdated too quickly. Instead, Python relies … Read more

Convert timestamps with offset to datetime obj using strptime

The Python 2 strptime() function indeed does not support the %z format for timezones (because the underlying time.strptime() function doesn’t support it). You have two options: Ignore the timezone when parsing with strptime: time_obj = datetime.datetime.strptime(time_str[:19], ‘%Y-%m-%dT%H:%M:%S’) use the dateutil module, it’s parse function does deal with timezones: from dateutil.parser import parse time_obj = parse(time_str) … Read more

How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?

Swift 4 • iOS 11.2.1 or later extension ISO8601DateFormatter { convenience init(_ formatOptions: Options) { self.init() self.formatOptions = formatOptions } } extension Formatter { static let iso8601withFractionalSeconds = ISO8601DateFormatter([.withInternetDateTime, .withFractionalSeconds]) } extension Date { var iso8601withFractionalSeconds: String { return Formatter.iso8601withFractionalSeconds.string(from: self) } } extension String { var iso8601withFractionalSeconds: Date? { return Formatter.iso8601withFractionalSeconds.date(from: self) } } … Read more