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

  • Home
  • SEARCH
  • 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 8476033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:04:01+00:00 2026-06-10T18:04:01+00:00

I am trying to convert ISO8601 time format to seconds/unix/epoch time in python with

  • 0

I am trying to convert ISO8601 time format to seconds/unix/epoch time in python with just using the standard library.

Time format = ‘2012-09-30T15:31:50.262-08:00’

Basically the time will be a string which it parses and converts it back to seconds.

Slicing and picking the values which we want is possible but is there any better way than this?

import datetime, time
def convert_enddate_to_seconds(self, ts):
    """Takes ISO 8601 format(string) and converts into epoch time."""
     dt = datetime.datetime.strptime(ts[:-7],'%Y-%m-%dT%H:%M:%S.%f')+\
                datetime.timedelta(hours=int(ts[-5:-3]),
                minutes=int(ts[-2:]))*int(ts[-6:-5]+'1')
    seconds = time.mktime(dt.timetuple()) + dt.microsecond/1000000.0
    return seconds
  • 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-10T18:04:02+00:00Added an answer on June 10, 2026 at 6:04 pm

    The final correct answer after Celada’s answer. Below I have verified that the outputs of both the xml.iso8601.parse also returns the same result as the one generated by the function. Thanks to Celada for providing issues with the original code.

    >>> import calendar, datetime, time 
    >>> def convert_enddate_to_seconds(ts):
    ...   """Takes ISO 8601 format(string) and converts into epoch time."""
    ...   dt = datetime.datetime.strptime(ts[:-7],'%Y-%m-%dT%H:%M:%S.%f')-\
    ...       datetime.timedelta(hours=int(ts[-5:-3]),
    ...       minutes=int(ts[-2:]))*int(ts[-6:-5]+'1')
    ...   seconds = calendar.timegm(dt.timetuple()) + dt.microsecond/1000000.0
    ...   return seconds
    ... 
    >>> import iso8601
    >>> ts = '2012-09-30T15:31:50.262-08:00'
    >>> iso8601.parse(ts)
    1349047910.0
    >>> convert_enddate_to_seconds(ts)
    1349047910.26
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to convert standard time variable from form input to TIME format acceptable for
I am trying to extract unix timestamp from mysql and convert it to ISO8601
I'm trying convert xml from one format to other using the XslCompiledTransform in c#.
Trying to convert a date & time value in the format of 07/19/2011 00:00
i trying to convert time in string format to minutes. i can use substring
I have a utc timestamp in the IS8601 format and am trying to convert
i´m using subsonic 3 trying convert a SQL2008 project to MySQL. when the projects
Trying to convert my local SVN repo to Git using svn2git as suggested by
im trying to convert a json object to a string by using gson here
I'm using lambda expression and trying convert into uint while adding into the Hashset.

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.