Login

Tag "datetime"

Snippet List

timeto template filter

This is a more compact version of django's [timeuntil](http://docs.djangoproject.com/en/dev/ref/templates/builtins/#timeuntil) filter that only shows hours & minutes. If used like `{{ dt|timeto }}`, will produce output like "1hr 30min". If you know for sure that the server has the same timezone as the [datetime](http://docs.python.org/library/datetime.html#datetime-objects) value, then you don't need [datetutil.tz](http://labix.org/python-dateutil#head-587bd3efc48f897f55c179abc520a34330ee0a62) for utc time conversion.

  • datetime
  • time
  • humanize
Read More

isoutc template filter

Use this template filter to produce an ISO format UTC datetime string from a [timezone aware](http://docs.python.org/library/datetime.html#datetime.tzinfo) [datetime](http://docs.python.org/library/datetime.html#datetime.datetime) object. Usage example in a template: `<input name="when" type="hidden" value="{{ dt|isoutc }}"`. You must have [dateutil](http://labix.org/python-dateutil) installed for `tz.tzutc()` to work. And of course, you'll need to load it as a [custom tag](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags) to use it. The output format differs from python's [datetime.isoformat](http://docs.python.org/library/datetime.html#datetime.datetime.isoformat) by ignoring the `microsecond` and including a "Z" suffix for the UTC timezone. For ease of use, it is also not double quoted as the standard suggests.

  • datetime
  • utc
Read More

FuzzyDateTimeField

FuzzyDateTimeField is a drop in replacement for the standard [DateTimeField](http://docs.djangoproject.com/en/dev/ref/forms/fields/#datetimefield) that uses [dateutil.parser](http://labix.org/python-dateutil#head-a23e8ae0a661d77b89dfb3476f85b26f0b30349c) to clean the value. It has an extra keyword argument `fuzzy=True`, which allows it to be more liberal with the input formats it accepts. Set `fuzzy=False` for more strict validation.

  • datetime
  • humanize
Read More

Smart i18n date diff (twitter like)

This snippet display a human readable date diff. You give it the your date in parameter and the diff with datetime.datetime.now() is returned. The diff must be positive to be more accurate (future dates are not supported) Usage: {{ status.created_at|date_diff }} Will give something like: less than 1 minute ago 13 minutes ago 1 hour ago etc. Based on [Fuzzy Date Diff Template Filter](http://www.djangosnippets.org/snippets/1347/)

  • datetime
  • i18n
  • date
  • diff
Read More

copyright_since

**Example usage**: {% copyright_since 2008 %}, {% copyright_since 2009 %} **Output**: © 2008—2009, © 2009 Use this templatetag in your footer to achieve proper formatting of copyright notice.

  • datetime
  • date
  • template-tag
  • formatting
  • copyright
  • copyright-formatting
  • copyright-year
Read More

Timedelta Database Field

This subclass of django.models.Field stores a python datetime.timedelta object as an integer column in the database. It includes a TimedeltaFormField for editing it through djangos admin interface. Feedback welcome. 2011-04-20: TimedeltaField can now be (de)serialized. Tested with JSON. 2009-11-23: `_has_changed()` added. Before form.changed_data contained timedelta FormFields, even if nothing changed.

  • datetime
  • timedelta
  • dbfield
Read More

auto_now using signals

There has been some discussion about removing `auto_now` and `auto_now_add` some time ago. `auto_now_add` can be replaced be using a callable default value, `auto_now` can't. So I wrote this litte function for my current project (older ones still use `auto_add`) to fill the gap...but I'm not sure if `auto_now` will be removed at all.

  • datetime
  • model
  • auto_now
Read More

Datetime widget

This widget uses: [DHTML Calendar Widget](http://www.dynarch.com/projects/calendar/). It is very simple implementation but may be easily extended/changed/refined. 1. Necessary files: First download calendar package and extract it to your MEDIA folder (MEDIA/calendar/...) You'll also need a small gif that will be shown as a button that allows user to display calendar. By default this 'gif' is searched at '[MEDIA]images/calbutton.gif' but you may change this path in the code (calbtn variable). You need to download or create callbutton.gif image by yourself (it is not included). 2. Include css and js files in your page (as shown in the comment in the code). 3. In form code assign a widget to a field as usual (see newforms documentation for more details). 4. It is possible to change date format by specifying different value for 'dformat' attribute of widget class. If you get javascript errors while trying to open calendar try to use english translation file (calendar-en.js). I've found that some translations, eg. Polish, are broken by default. In this case you should override your language translation with english one and translate it by yourself (it is easy).

  • datetime
  • date
  • calendar
  • widget
  • dhtml
Read More

UTC DateTime field

A DateTime field extension that automatically stores the timezone, and the computed UTC equivalent. This field needs the pytz library. The field adds two new fields to the model, with the same name of the UTCDateTimeField field, and a suffix. For an UTCDateTimeField named 'updated', the model will contain * an 'updated' field, which holds the local datetime * an 'updated_utc' field, which holds the corresponding UTC datetime * an 'updated_tz' field, which holds the field timezone name The timezone can vary between model instances, just set the 'xxx_tz' field to the desired timezone before saving. UTCDateTimeField supports a single optional keyword argument 'default_tz', in addition to the DateTimeField standard ones, to let the user choose a provider for a default timezone when no timezone has been set. Its value can be * None (or the argument missing), in which case the default settings.TIME_ZONE will be used * a callable, which will be called when the 'xxx_tz' field is emtpy, which should return a timezone name * a string, which will be used to access a model attribute or call a model method, which should return a timezone name If the timezone name points to a non-existent timezone, a warning will be issued and the default settings.TIME_ZONE value will be used. The field will also add three properties to the model, to access the pytz timezone instance, and the offset aware datetimes for local time and UTC. For the same 'updated' field instance we used above, the three properties added to the model will be: * updated_timezone * updated_offset_aware * updated_utc_offset_aware A brief example on how to use UTCDateTimeField: class UTCDateTimeTest(models.Model): """ >>> import datetime >>> d = datetime.datetime(2007, 8, 24, 16, 46, 34, 762627) # new instance, tz from model method >>> t = UTCDateTimeTest(updated=d) >>> t.save() >>> t.updated datetime.datetime(2007, 8, 24, 16, 46, 34, 762627) >>> t.updated_utc datetime.datetime(2007, 8, 24, 14, 46, 34, 762627) >>> t.updated_tz 'Europe/Rome' >>> t.updated_timezone <DstTzInfo 'Europe/Rome' CET+1:00:00 STD> >>> t.updated_offset_aware datetime.datetime(2007, 8, 24, 16, 46, 34, 762627, tzinfo=<DstTzInfo 'Europe/Rome' CEST+2:00:00 DST>) >>> t.updated_utc_offset_aware datetime.datetime(2007, 8, 24, 14, 46, 34, 762627, tzinfo=<UTC>) >>> """ updated = UTCDateTimeField(default_tz='get_tz') def get_tz(self): return 'Europe/Rome'

  • models
  • fields
  • datetime
  • timezone
  • field
  • utc
Read More

Natural language date/time form fields

Form fields use the dateutil module [http://labix.org/python-dateutil](http://labix.org/python-dateutil) to parse natural-language input for date and datetime fields. The callback function will replace all date and datetime fields automatically for form_for_model and form_for_instance. **Note**: by replacing the 'form_class' keyword argument instead of just returning the field itself you preserve the 'required' status of the field.

  • newforms
  • datetime
  • date
Read More

Client-side Django-style date & time string formatting

This is a reasonably straight forward port of functionality provided by the `django.utils.dateformat` module into a method extending JavaScript's Date object. Its intended use is to allow client-side dynamic content to share the same date & time string formatting as Django template markup. By using this in conjunction with a context processor (to pass a format string to all templates) you can switch formats for both server-generated & client-generated dates across a complete site with a single setting. The function supports *almost* the entire format -- as listed by the Django documentation for the [now template tag](http://www.djangoproject.com/documentation/templates/#now) -- with the exception of "I" & "T". As a 'dumb' illustration, the following template tag usage: It is {% now "jS F Y H:i" %} ...could equate to the following: It is <script type="text/javascript">var now = new Date(); document.write(now.strfdate('jS F Y H:i'));</script> It's not extensively tested (I only wrote it over the weekend), but seems to be working okay. Feel free to leave any corrections or suggestions in the comments, and I'll try to keep this entry updated if I make any fixes or changes.

  • javascript
  • dynamic
  • datetime
  • date
  • format
  • time
  • string
  • now
Read More

29 snippets posted so far.