I am making a blog and store the publishing date of a blog post in the datastore. It looks like this:
post.date = datetime.datetime.now()
It now displays like: 2010-10-04 07:30:15.204352 But I want the datetime to be displayed differently. How (and where) can I set that how the date is displayed? I'd like to set the date format like in UNIX date function (like %Y/%m etc). I tried to add some parameters in my templates but that returned errors.
Thanks in advance!
-skazhy
I think strftime is the method you're looking for.
From the link:
>>> d.strftime("%d/%m/%y")
'11/03/02'
If you pass in the result of the strftime in your 'template_values' or similar (the dictionary you use to pass parameters to the template) instead of the actual date it will be displayed instead.
You can use .strftime() on a datetime object to do the formatting. See the relevant python documentation for details.
Related
class Test(Model):
time = DateTimeField()
# ...
row = Test.select()[0]
test.time
This returns a string that looks like this: 2017-01-23 01:01:39+01:00. How can I get it as a datetime object instead? Do I have to parse it manually?
Also I would be interested if there is any documentation on how to use the DateTimeField. The official documentation doesn't have anything on it.
Are you using SQLite? If so, SQLite doesn't have a dedicated datetime type, so datetimes are stored as strings in the DB. What peewee will do is recognize certain datetime formats coming out of the DB and convert them to datetime objects. What you need to do is ensure that either:
When you create/save your object, that you assign a datetime object to the field.
When reading back pre-existing data, that the data is in a recognized format.
The formats peewee supports out-of-the-box for datetime field are:
YYYY-mm-dd HH:MM:SS.ffffff
YYYY-mm-dd HH:MM:SS
YYYY-mm-dd
It looks like your has zone info. I'd suggest converting to UTC and dropping the zone info. That should fix it.
Have you tried adding a default like this?
time = DateTimeField(default=datetime.datetime.now())
Or when adding an entry add it as a datetime.datetime object directly:
test = Test(....., time=datetime.datetime.strptime("2018-3-15", '%Y-%m-%d'))
In the second case you don't need to specify anything in the class definition...
I have start_date of DateField() which is in utc.I want to convert it to timezone aware object
I tried writing
{{ start_date|timezone:'Asia/Calcutta' }}
It displays nothing.Is timezone only for DateTimeField()?How can I use it on DateField().
Yes, unfortunately, this only works for datetime objects (s. https://github.com/django/django/blob/abaf0ab4a444977dc8ac07b9b63256814c352245/django/templatetags/tz.py#L46)
As far as I know, there's no real way to make a date object timezone-aware. You can convert a date object to datetime though (s. https://stackoverflow.com/a/1937636/246028)
If you tell us what you're trying to achieve by making a date timezone-aware, I can try to amend my answer.
I am trying to iterate over a model's attributes, getting both the verbose name and value for each attribute. I have no problem for character/numerical values, but I am struggling with DateTime values. Of course I can do this manually, but some of my models have dozens of attributes (only a few of which are DateTimes), so it would be nice if I could figure out how to do this programatically.
My code currently (with the 'History' Model class):
def get_fields(self):
return [(field.verbose_name, field.value_to_string(self)) for field in History._meta.fields]
In the case of a DateTime, of course this outputs a string, such as: 2011-06-16T04:00:00+00:00
Suggestions on pulling out DateTime values? Apologies if this is a very basic question -- I have been using Django for about a year, but am more recently trying to tackle area's I am unfamiliar with. Thank you!
You can use strftime() on datetime objects to specify how the string representation should be formatted, but you probably have to override the value_to_string() method in the datetimefield if you want to iterate over all fields in a uniform way.
See python doc of datetime.strftime() here:
https://docs.python.org/2/library/datetime.html
I need to parse a date/time string from user input, and convert to UTC based on timzeone info not available in the string for datetime.strptime() (any suggestions?). Is there a straightforward way of doing this?
Ideally, on google app engine i'd like to grab local time with tzinfo from the browser if possible also.
timezone_string = "GMT-0800"
fields = ("eventstartmonth","eventstartday", "eventstartyear", "eventstarttimehour", "eventstarttimeampm")
date_string = '_'.join(map(lambda x: self.request.get(x), fields))
# date_string = "01_11_2000_1:35_PM"
dt = datetime.datetime.strptime(date_string, "%m_%d_%Y_%I:%M_%p")
# how to convert dt into a tz-aware datetime, and then to UTC
While searching for similar information I came across a demo app engine app (with source included) that demonstrates how to convert timezones in a way that's similar to what you've requested. Unfortunately, though, you'll need to create custom tzinfo classes (explanation/code in the demo app linked above) for each timezone you'll be converting.
If you need to be able to handle any timezone and/or want to take the easy route, I'd recommend using the pytz module. However, keep in mind, pytz is a rather bulky module that you'd have to upload to your GAE instance.
I am reading a Facebook updates feed using the python library 'feedparser'.
I loop through the collection of entries in my Django templates, and display the results.
The updated field is returned in a big long string, of some format I am unfamiliar with.
Tue, 01 Dec 2009 23:55:52 +0000
How can I...
A) Use a Django filter to clean the date time in the for loop on the template.
...or...
B) Parse the date and format the updated date in the view, esentially cleaning the date in the collection of entries before it is iterated over in the view.
NOTE: I have tried both approaches. Django's date filter does't recognize it, and the iso8601 library I tried to parse the string didn't either.
Anybody have any experience with this? Thanks for your help!
UPDATE:
Using the updated_parsed value from feedparser in a Django template didn't work so well. But a Django snippet of a filter for this very thing already exists!**
Django Snippet: http://www.djangosnippets.org/snippets/1595/
Use entries[i].updated_parsed instead of entries[i].updated, and feedparser will return a parsed 9-tuple for you. (Documentation)
Then build a datetime object and pass it to Django or format to a string by yourself.
There is a similar question here.
This worked but wasn't what my final solution ended up becoming.
This solution iterates over the feed entries collection I get back from Facebook. I then parse the datetime and set the updated property to that new datetime. (Also, ignoring the +0000)
for entry in feed.entries:
entry.updated = datetime.strptime(entry.updated, "%a, %d %b %Y %H:%M:%S +0000")
The entries collection is returned to the template which I can now use the Django 'date' filter to format the date.