Going from twitter date to Python datetime date

前端 未结 10 1283
谎友^
谎友^ 2020-12-23 09:21

I am receiving twitter messages that are sent at a certain date in the following format from twitter:

Tue Mar 29 08:11:25 +0000 2011

I want

相关标签:
10条回答
  • 2020-12-23 09:54

    The following code will print a nice date (local time) from a Twitter date (UTC).

    from datetime import datetime
    from datetime import timezone    
    
    datetime.strptime(mydata["created_at"], '%a %b %d %H:%M:%S %z %Y').replace(
                tzinfo=timezone.utc).astimezone(tz=None).strftime('%Y-%m-%d %H:%M:%S'))
    
    0 讨论(0)
  • 2020-12-23 09:55

    Using a similar strategy as SoFolichon proposed, in Python 3.x you can also use pytz like:

    from datetime import datetime, timezone
    import pytz
    
    datetime.strptime(tweets["created_at"], '%a %b %d %H:%M:%S %z %Y').replace(
    tzinfo=timezone.utc).astimezone(pytz.timezone('US/Eastern')).strftime(
    '%Y-%m-%d %H:%M:%S')
    
    0 讨论(0)
  • 2020-12-23 09:56

    Give this a go. It assumes the date format from twitter is RFC822 compliant (see the question linked to by @Adrien).

    A naive datetime object is constructed (i.e. no timezone info). It is adjusted according to the timezone offset to UTC. Unless you have a need to keep the original timezone, I'd store the date time as UTC and format to local time when you display it.

    from datetime import datetime, timedelta
    from email.utils import parsedate_tz
    
    s = 'Tue Mar 29 08:11:25 +0000 2011'
    
    def to_datetime(datestring):
        time_tuple = parsedate_tz(datestring.strip())
        dt = datetime(*time_tuple[:6])
        return dt - timedelta(seconds=time_tuple[-1])
    
    0 讨论(0)
  • 2020-12-23 09:58

    How about this? It doesn't need any formatting strings.

    import datetime
    from email.utils import mktime_tz, parsedate_tz
    
    def parse_datetime(value):
        time_tuple = parsedate_tz(value)
        timestamp = mktime_tz(time_tuple)
    
        return datetime.datetime.fromtimestamp(timestamp)
    
    print(parse_datetime('Tue Mar 29 08:11:25 +0000 2011'))
    #2011-03-29 10:11:25
    

    My system is at GMT +2 hence the difference included.

    0 讨论(0)
  • 2020-12-23 10:04

    Writing something like this should convert a twitter date to a timestamp.

    import time
    
    ts = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y'))
    

    UPDATE

    For Python 3, as per 2020, you can do it in this way:

    from datetime import datetime
    
    # dtime = tweet['created_at']
    dtime = 'Fri Oct 09 10:01:41 +0000 2015'
    new_datetime = datetime.strftime(datetime.strptime(dtime,'%a %b %d %H:%M:%S +0000 %Y'), '%Y-%m-%d %H:%M:%S')
    print((new_datetime))
    
    0 讨论(0)
  • 2020-12-23 10:06

    A little bit old but using parse really help me with this issue

    from datetime import datetime
    from dateutil.parser import parse
    
    date = 'Fri May 10 00:44:04 +0000 2019' 
    dt = parse(date)
    
    print(dt) 
    # 2019-05-10 00:44:04+00:00
    
    0 讨论(0)
提交回复
热议问题