djangosnippets.org: Latest snippets tagged with 'datetime'https://djangosnippets.org/tags/datetime/2012-05-27T10:18:44-05:00Querying datetime aware objects in your local timezone2012-05-27T10:18:44-05:00jayliewhttps://djangosnippets.org/snippets/2759/<p>I have a model with a datetime field that I used as a timestamp. I’m in California’s timezone (“America/Los_Angeles”). The data is saved in UTC in MySQL (as confirmed by the ORM). I just want to do a query that looks like this: “give me all the ...</p>
Freely redistributabledatemate2011-08-24T17:39:38-05:00catellarhttps://djangosnippets.org/snippets/2530/<p>This wil format the date to today at 1:03 pm , yesterday at 9:13 pm, 22 August at 10:08 pm</p>
Freely redistributableastimezone template tag2011-03-09T14:57:38-06:00whardierhttps://djangosnippets.org/snippets/2388/<p>should probably be migrated to an inclusion tag to allow a source timezone that isn't the site specific TIME_ZONE.</p>
<p>This code assumes that your database stores dates according to the django.conf.settings.TIME_ZONE variable.</p>
<p>Yes.. this assumes that dates are stored in the database according to system time ...</p>
Freely redistributableDateTimeFluxCapacitor2011-01-29T21:26:41-06:00jbcurtinhttps://djangosnippets.org/snippets/2339/<p>Takes the current DateTime and adds either days, hours, minutes, or seconds to the datetime object.
</p>
Freely redistributableSelectDateWidget2010-12-15T16:08:16-06:00silent1mezzohttps://djangosnippets.org/snippets/2289/<p>This widget will produce a select box with the range of dates that you input.</p>
<p><strong>Usage:</strong></p>
<p><code>widget=SelectDateWidget('2010-12-15', '2010-12-20')</code></p>
<p><strong>Output:</strong></p>
<p>`&lt;select&gt;</p>
<p>&lt;option value="2010-12-15"&gt;Wed January 01, 2010&lt;/option&gt;</p>
<p>&lt;option value="2010-12-16"&gt;Thu January 02, 2010&lt;/option&gt;</p>
<p>&lt;option value="2010-12-17"&gt;Fri January 03, 2010&lt;/option&gt;</p>
<p>&lt;option value="2010-12-18"&gt;Sat ...</p>
Freely redistributableParse datetime model field to string2010-07-03T02:59:18-05:00jzelezhttps://djangosnippets.org/snippets/2090/<p>Checks the type of the field (date / time / date-time) and returns corresponding value as a string.</p>
Freely redistributabledatetime.time/datetime.datetime to Unix Epoch (with microsecond support)2010-04-27T05:40:41-05:00sleepycalhttps://djangosnippets.org/snippets/1997/<p>This is useful when you need to convert a datetime.datetime.now() or datetime.date.today() into a unix epoch seconds, with microsecond precision (precision only applies to datetime.datetime, as datetime.date won't have any microseconds).</p>
<p>I have found this is necessary for when storing the DateTime in ...</p>
Freely redistributableparse date template tag2009-10-13T13:17:21-05:00robhudsonhttps://djangosnippets.org/snippets/1757/<p>Return a datetime corresponding to date_string, parsed according to format.</p>
<p>I had the need for such a thing while working with an API that returned JSON that I fed, via simplejson, directly to a template, and didn't want to change the data structure just for this one piece.</p>
Freely redistributableDateTimeWidget using JSCal22009-07-13T09:14:59-05:00ramusushttps://djangosnippets.org/snippets/1629/<p>DateTimeWidget using <a href="http://www.dynarch.com/projects/calendar/">JSCal2</a></p>
<p>Duplicate of <a href="http://www.djangosnippets.org/snippets/391/">this snippet</a>, but for latest 1.5 version of DHTML Calendar.</p>
<p>Also here is <strong>fixed problem of previous widget</strong> linked to <em>form.changed_data</em> and <em>EntryLog.message</em>. This is fixed by adding own, little modified <em>_has_changed()</em> method</p>
Freely redistributableTemplate-Filter for Feedparser-Dates2009-06-25T11:33:20-05:00kioopihttps://djangosnippets.org/snippets/1595/<p>A filter that changes a preparsed date from <a href="http://www.feedparser.org/">Ultimate Feedparser</a> to a regular datetime instance. </p>
<p>Now you can -for example- pass a feed parsed by feedparser to a template and do this:</p>
<pre><code>{% for item in feed.entries %}
Title: {{ item.title }}&lt;br /&gt;
Date: {{ item.updated_parsed|feedparsed|date:"Y-m-d" }}
{% endfor %}
</code></pre>
Freely redistributableJSON decode datetime2009-04-13T09:14:10-05:00japerkhttps://djangosnippets.org/snippets/1436/<p>If you have JSON objects with <code>datetime</code> attributes that you want to decode to python <a href="http://docs.python.org/library/datetime.html#datetime.datetime">datetime</a> objects, you can use <code>decode_datetime</code> as a <a href="http://simplejson.googlecode.com/svn/tags/simplejson-2.0.9/docs/index.html">simplejson</a> object hook. <code>simplejson.loads(s, object_hook=decode_datetime)</code>.</p>
Freely redistributableJSON encode ISO UTC datetime2009-04-13T09:04:51-05:00japerkhttps://djangosnippets.org/snippets/1435/<p>If you want to do your own JSON serialization of <a href="http://docs.python.org/library/datetime.html#datetime.datetime">datetime</a> objects instead of using DjangoJSONEncoder, use <code>simplejson.dumps(o, default=encode_datetime)</code>. The <code>encode_datetime</code> method will convert the datetime object to UTC and output an ISO format string just like the <a href="http://www.djangosnippets.org/snippets/1424/">isoutc template filter</a>.</p>
Freely redistributableHTML/JS template filter to show localized dates/times2009-04-12T11:13:00-05:00mback2khttps://djangosnippets.org/snippets/1433/<p>While working on my website projects today I had the idea to use HTML/JS instead of a IP database to localize the dates and times shown on the websites.</p>
<p>Here are the snippets to use the JS snippets as filters for Django running on Google App Engine. You can ...</p>
Freely redistributabletimeto template filter2009-04-10T11:27:30-05:00japerkhttps://djangosnippets.org/snippets/1426/<p>This is a more compact version of django's <a href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/#timeuntil">timeuntil</a> filter that only shows hours &amp; minutes. If used like <code>{{ dt|timeto }}</code>, will produce output like "1hr 30min". If you know for sure that the server has the same timezone as the <a href="http://docs.python.org/library/datetime.html#datetime-objects">datetime</a> value, then you don't need <a href="http://labix.org/python-dateutil#head-587bd3efc48f897f55c179abc520a34330ee0a62">datetutil.tz ...</a></p>
Freely redistributableisoutc template filter2009-04-09T21:32:13-05:00japerkhttps://djangosnippets.org/snippets/1424/<p>Use this template filter to produce an ISO format UTC datetime string from a <a href="http://docs.python.org/library/datetime.html#datetime.tzinfo">timezone aware</a> <a href="http://docs.python.org/library/datetime.html#datetime.datetime">datetime</a> object. Usage example in a template:
<code>&lt;input name="when" type="hidden" value="{{ dt|isoutc }}"</code>.
You must have <a href="http://labix.org/python-dateutil">dateutil</a> installed for <code>tz.tzutc()</code> to work. And of course, you'll need to load it ...</p>
Freely redistributable