Skip to main content

Posts

Showing posts with the label date

JavaScript: validate date field in form

You may often require serialization of a form in JS and parsing of it's data. Here is a nice recipe to validate date with browser default methods. I like it because of relative simplicity. It works on a principle of parsing date's components. We will create a JavaScript Date object from it and check if it is the same as a parsed date string. Date components that are wrong will go out of rage. SO we will have them different from the existing strings. This method is based on that hack. I use this only to validate the string itself. I am specifying a placeholder of an input field to minimise user possibility to make a mistake. And I usually force user to enter date in my format with this tool. Anyway it's a good idea to put any datepicker component also. function validate_date ( value ) { /**********************************************     * Validating date in format dd/mm/yyyy     ************************************...

Timezones. New in Django 1.4.

What and Why? Under Django version 1.4 all dates stored and displayed for a single timezone. The one that is specified in project.settings.TIME_ZONE . Now you gain ability to store dates in UTC and render it with timezone correction. Problems with localtime  bypass is an additional plus. They can happen once a year. For e.g. 31 of November 2012 in Russia. Time from 2:00am to 3:00am in fact goes by twice. It may not be a problem for 99% of users. But it can become a nightmare for billing systems. So it's better to store time in UTC and display with user Time Zone correction. So "02:15am 31 November 2012" will become " 2012-10-30T22:15:00+04:00" and "2010-10-30T23:15:00+03:00" that is so handy for programmers ;). Concepts datetime objects in Python support timezones with attribute tzinfo . If this attribute is filled out it is called "timezone-aware", otherwise it's "naive" date. Django uses timezone-aware date...

Django: Adding news archive. Querying models with date and time filtering.

Today I had some practice with filtering Django models by date. Task was to archive old entries of the news. I've made 2 separate views for those. I was displaying actually news and another was ment to display archived items. Quite common task I guess. Also those 2 views are under wired buggy CMS, but we are not talking about that now. Will save some examples for future of mine/someones usage. Time. Subtracting a year from today with Python dateutil : from datetime import date , timedelta d = date . today ( ) - timedelta ( days = days_to_subtract ) This example returns 'd' that has less days than specified in 'days_to_subtract' . timedelta function also can be fed with 'months' , 'years' and so on. Becomes handy during time calculations. If you change this minus into plus you can calculate future time. Filtering Querysets. Filtering by date ranges. samples = Sample . objects . filter ( date__gte = datetime . date ( 2011 , 1...