Method Name:
isoformat
Method Signature:
isoformat(sep='T', timespec='auto')
Overview:
- isoformat() method of datetime class returns a date,time string which contains the following information:
- Date
- Time
- UTC offset to corresponding time zone
as specified in the standard ISO 8601.
- The separator character will be printed between the date and time fields.
Parameters:
sep - The separator character that is to be printed between the date and time fields. This is an Optional Parameter with a dfault value of "T".
timespec - Format specifier for the timespec. This is an Optional Parameter with a dfault value of "auto".
The values are,
auto: When the value is auto, the time component will be printed in HH:MM:SS format. If microseconds component is available it will be printed. Otherwise, microseconds will be omitted instead of printing as zero.
hours: When the value is hours, the time component will have only Hours in HH format. Note that, time zone component is different from time component.
minutes: If minutes is specified, the time component will have only the Hours and Minutes printed in HH:MM format.
seconds: If seconds is specified, the time component will be printed in HH:MM:SS format.
milliseconds: If milliseconds is specified, the time component will be printed in HH:MM:SS:mmm format, where mmm is milliseconds. Microseconds will be excluded.
microseconds: If microseconds is specified, the time the time component will be printed in HH:MM:mmmmmm format, where mmmmmm is microseconds.
Example:
import datetime # Local time without timezone information printed in ISO 8601 format # Date time separator is a "#" symbol # Create a timedelta object for CST time zone # Create a timezone instance for CST time zone # Replace the time zone with CST # Print CST in ISO format - date time separator is a "#" symbol # Time format specifier as hours # Time format specifier as minutes # Time format specifier as seconds # Time format specifier as milliseconds # Time format specifier as microseconds |
Output:
2017-02-15|20:26:08.937881 2017-02-15#20:26:08.937881-06:00 2017-02-15#20-06:00 2017-02-15#20:26-06:00 2017-02-15#20:26:08-06:00 2017-02-15#20:26:08.937-06:00 2017-02-15#20:26:08.937881-06:00 |