djangosnippets.org: Latest snippets tagged with 'datetime'https://djangosnippets.org/tags/datetime/2016-04-17T07:22:17.604370-05:00Translate datetime format strings from Python to PHP
2016-04-17T07:22:17.604370-05:00bernd-wechnerhttps://djangosnippets.org/snippets/10563/<p>A simple Python function that converts a Python datetime formatting string to its nearest PHP equivalent. </p>
<p>Python and PHP use different format string conventions when specifying datetime formats:</p>
<ul>
<li>Python: <a href="https://docs.python.org/2/library/time.html#time.strftime">https://docs.python.org/2/library/time.html#time.strftime</a> </li>
<li>PHP: <a href="http://php.net/manual/en/function.date.php">http://php.net/manual/en/function.date.php</a></li>
</ul>
<p>Working with Django the Date, Time and DateTime widgets all use Python format strings as stored in:</p>
<ul>
<li>django.conf.global_settings.DATE_INPUT_FORMATS …</li></ul>
Freely redistributableDatetime adjuster for Django Templates
2015-07-22T08:54:23.899048-05:00McAnixhttps://djangosnippets.org/snippets/10516/<p>The filter is <strong>specific to datetime objects</strong> and no allowance has been made to convert strings or epoch times. Also <strong>no type checking</strong> is performed so misuse will result in an error.</p>
<p>To use include the above snippet in a file called templatetags/customfilters.py or append to existing filters file then …</p>
Freely redistributableQuerying datetime aware objects in your local timezone
2012-05-27T10:18:44.805557-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 information with day …</p>
Freely redistributabledatemate
2011-08-24T17:39:38.890221-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 tag
2011-03-09T14:57:38.909743-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. On my systems the …</p>
Freely redistributableDateTimeFluxCapacitor
2011-01-29T21:26:41.482476-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 redistributableSelectDateWidget
2010-12-15T16:08:16.139743-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>`<select></p>
<p><option value="2010-12-15">Wed January 01, 2010</option></p>
<p><option value="2010-12-16">Thu January 02, 2010</option></p>
<p><option value="2010-12-17">Fri January 03, 2010</option></p>
<p><option value="2010-12-18">Sat January 04, 2010</option></p>
<p><option value="2010-12-19">Sun January 05, 2010</option></p>
<p><option value="2010-12-20">Mon January 06, 2010</option> …</p>
Freely redistributableParse datetime model field to string
2010-07-03T02:59:18.553331-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.710824-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 the models as a FloatField, in order …</p>
Freely redistributableparse date template tag
2009-10-13T13:17:21.332095-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 JSCal2
2009-07-13T09:14:59.759891-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-Dates
2009-06-25T11:33:20.974365-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 }}<br />
Date: {{ item.updated_parsed|feedparsed|date:"Y-m-d" }}
{% endfor %}
</code></pre>
Freely redistributableJSON decode datetime
2009-04-13T09:14:10.613597-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 datetime
2009-04-13T09:04:51.647479-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/times
2009-04-12T11:13:00.286858-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 use …</p>
Freely redistributable