Convert string to timestamp python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The Most Common way we use to store dates and times into a Database is in the form of a timestamp. When we receive a date and time in form of a string before storing it into a database, we convert that date and time string into a timestamp. Python provides various ways of converting the date to timestamp. Few of them discussed in this article are:

    • Using timetuple()
    • Using timestamp()

    Using timetuple()

    Approach:

    • import datetime is use to import the date and time modules
    • After importing date time module next step is to accept date string as an input and then store it into a variable
    • Then we use strptime method. This method takes two arguments:
      • First argument is the string format of the date and time
      • Second argument is the format of the input string
    • We convert date and time string into a date object
    • Then we use timetuple() to convert date object into tuple
    • At the end we use mktime(tuple) to convert the Date tuple into a timestamp.

    Example 1:

    import time

    import datetime

    string = "20/01/2020"

    print(time.mktime(datetime.datetime.strptime(string,

                                                 "%d/%m/%Y").timetuple()))

    Output:

    1579458600.0
    

    Example 2:

    import time

    import datetime

    string = "20/01/2020"

    element = datetime.datetime.strptime(string,"%d/%m/%Y")

    tuple = element.timetuple()

    timestamp = time.mktime(tuple)

    print(timestamp)

    Output:

    1579458600.0
    

    Using timestamp()

    Approach:

    • import datetime is use to import the date and time modules
    • After importing date time module next step is to accept date string as an input and then store it into a variable
    • Then we use strptime method. This method takes two arguments
      • First arguments s the string format of the date and time
      • Second argument is the format of the input string
    • Then it returns date and time value in timestamp format and we stored this in timestamp variable.

    Example 1:

    import time

    import datetime

    string = "20/01/2020"

    element = datetime.datetime.strptime(string,"%d/%m/%Y")

    timestamp = datetime.datetime.timestamp(element)

    print(timestamp)

    Output:

    1579458600.0
    

    A lot of these answers don't bother to consider that the date is naive to begin with

    To be correct, you need to make the naive date a timezone aware datetime first

    import datetime
    import pytz
    # naive datetime
    d = datetime.datetime.strptime('01/12/2011', '%d/%m/%Y')
    >>> datetime.datetime(2011, 12, 1, 0, 0)
    
    # add proper timezone
    pst = pytz.timezone('America/Los_Angeles')
    d = pst.localize(d)
    >>> datetime.datetime(2011, 12, 1, 0, 0,
    tzinfo=)
    
    # convert to UTC timezone
    utc = pytz.UTC
    d = d.astimezone(utc)
    >>> datetime.datetime(2011, 12, 1, 8, 0, tzinfo=)
    
    # epoch is the beginning of time in the UTC timestamp world
    epoch = datetime.datetime(1970,1,1,0,0,0,tzinfo=pytz.UTC)
    >>> datetime.datetime(1970, 1, 1, 0, 0, tzinfo=)
    
    # get the total second difference
    ts = (d - epoch).total_seconds()
    >>> 1322726400.0
    

    Also:

    Be careful, using pytz for tzinfo in a datetime.datetime DOESN'T WORK for many timezones. See datetime with pytz timezone. Different offset depending on how tzinfo is set

    # Don't do this:
    d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
    >>> datetime.datetime(2011, 1, 12, 0, 0, 
    tzinfo=)
    # tzinfo in not PST but LMT here, with a 7min offset !!!
    
    # when converting to UTC:
    d = d.astimezone(pytz.UTC)
    >>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
    # you end up with an offset
    

    https://en.wikipedia.org/wiki/Local_mean_time

    How do I convert a string to a date timestamp in Python?

    Approach:.
    import datetime is use to import the date and time modules..
    After importing date time module next step is to accept date string as an input and then store it into a variable..
    Then we use strptime method. ... .
    We convert date and time string into a date object..
    Then we use timetuple() to convert date object into tuple..

    How do I convert a string to time stamp?

    To convert a date string to a timestamp: Pass the date string to the Date() constructor. Call the getTime() method on the Date object. The getTime method returns the number of milliseconds since the Unix Epoch.

    How do I convert a string to a datetime object in Python?

    To convert string to datetime in Python, use the strptime() method. The strptime() is a built-in function of the Python datetime class used to convert a string representation of the​ date/time to a date object.

    How do I make a timestamp in Python?

    timegm() method to convert the current time to the timestamp..
    First, import both time and the calendar modules..
    Next, get the GMT time using the time module's time. gmtime() method..
    At last, pass it to the Use the calendar.timegm() method to get a timestamp..