98π
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))
15π
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])
- [Django]-Error when using django.template
- [Django]-HTML β How to do a Confirmation popup to a Submit button and then send the request?
- [Django]-One-to-many inline select with django admin
8π
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
- [Django]-Django: How can I protect against concurrent modification of database entries
- [Django]-Profiling Django
- [Django]-How to use subquery in django?
3π
To get datetime with timezone you can simple use datetime.strptime as follow:
from datetime import datetime
s = 'Wed Jun 05 05:34:02 +0000 2019'
created_at = datetime.strptime(s, '%a %b %d %H:%M:%S %z %Y')
print(created_at)
#2019-06-05 05:34:02+00:00
- [Django]-Inline Form Validation in Django
- [Django]-How to disable Django's CSRF validation?
- [Django]-Catching DoesNotExist exception in a custom manager in Django
3π
Twitter API V2 sends date strings that look like this:
2020-12-15T20:17:10.000Z
This worked, to convert from string to datetime:
datetime.datetime.strptime(THE_STRING,"%Y-%m-%dT%H:%M:%S.%fZ")
The end looks like a timezone, but itβs milliseconds, hence the %f. The final character, "Z" is a timezone code that means UTC, as explained, here.
- [Django]-In Django models.py, what's the difference between default, null, and blank?
- [Django]-Django select only rows with duplicate field values
- [Django]-Getting Site Matching Query Does Not Exist Error after creating django admin
2π
you can convert the date using datetime.strptime()
, or time.strptime()
. however, those two functions cannot parse the timezone offset (see this bug).
so, the only solution i see is to split the date yourself, remove the timezone offset, feed the rest to strptime()
, and process the offset manuallyβ¦
have a look at this question, where you will find some hints on how to parse the offset yourself.
- [Django]-Stack trace from manage.py runserver not appearing
- [Django]-Celery missed heartbeat (on_node_lost)
- [Django]-Could not find a version that satisfies the requirement pkg-resources==0.0.0
1π
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'))
- [Django]-How to make the foreign key field optional in Django model?
- [Django]-The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
- [Django]-Assign variables to child template in {% include %} tag Django
1π
The initial problem I was having was converting from the datetime that the twitter api gives to String.
The following works which addresses different comments people seem to have for above solutions which are a little unclear as to whether the starting date is already in string format or not.
This works for Python 2.7
With a tweet from the API, tweet.created_at gives the date in datetime format. At the top of your file, add from datetime import datetime
then use the following to get the corresponding string.
datetime.strftime(tweet.created_at,'%a %b %d %H:%M:%S %z %Y').
You can then use this string as described in other comments to manipulate it.
- [Django]-How to use the 'reverse' of a Django ManyToMany relationship?
- [Django]-How to query as GROUP BY in Django?
- [Django]-Django β Render the <label> of a single form field
0π
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')
- [Django]-What's the best solution for OpenID with Django?
- [Django]-Django REST Framework: adding additional field to ModelSerializer
- [Django]-Get count of related model efficiently in Django
0π
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.
- [Django]-No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model
- [Django]-How to pull a random record using Django's ORM?
- [Django]-Cannot import name patterns