10 examples of 'django date format' in Python

Every line of 'django date format' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
44@register.function
45def dj_date(value, format_string):
46 return django_date(value, format_string)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

332def date(value, arg=DATE_FORMAT):
333 "Formats a date according to the given format"
334 if value in EMPTY_DATE_VALUES:
335 return ''
336 from django.utils.dateformat import format
337 return format(value, arg)
620def date(value, arg=None):
621 """Formats a date according to the given format."""
622 from django.utils.dateformat import format
623 if not value:
624 return u''
625 if arg is None:
626 arg = settings.DATE_FORMAT
627 return format(value, arg)
7@register.filter
8def smart_date(value):
9 now = timezone.localtime(timezone.now(), value.tzinfo)
10 if value.year == now.year:
11 if value.month == now.month and value.day == now.day:
12 return value.strftime('%H:%M')
13 return value.strftime('%b %d')
14 return value.strftime('%b %d, %Y')
421def test_dateField(self):
422 "DateFields can parse dates in the default format"
423 f = forms.DateField()
424 # Parse a date in an unaccepted format; get an error
425 self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
426
427 # Parse a date in a valid format, get a parsed result
428 result = f.clean('21.12.2010')
429 self.assertEqual(result, date(2010,12,21))
430
431 # Check that the parsed result does a round trip
432 text = f.widget._format_value(result)
433 self.assertEqual(text, '21.12.2010')
434
435 # Parse a date in a valid, but non-default format, get a parsed result
436 result = f.clean('21-12-2010')
437 self.assertEqual(result, date(2010,12,21))
438
439 # Check that the parsed result does a round trip to default format
440 text = f.widget._format_value(result)
441 self.assertEqual(text, "21.12.2010")
45def date_format(context):
46 """ Returns Context Variable Containing Date format currently used """
47 return {'DATE_FORMAT': settings.DATE_FORMAT.replace('%', '')}
6def format_date(date_object):
7 """Formats the date and returns the datetime object"""
8 # date_time = date_object.split("+")
9 return datetime.strptime(str(date_object), "%Y-%m-%dT%H:%M:%S")
96def date_and_time(value, formatting="N j, Y, h:i a"):
97 """
98 Reformats a date string in a humanized format, AP style by default.
99 """
100 try:
101 dt = dateparse(value)
102 except ValueError:
103 return value
104 return dateformater(dt, formatting)
385def _format_value(self, value):
386 if self.is_localized and not self.manual_format:
387 return formats.localize_input(value)
388 elif hasattr(value, 'strftime'):
389 value = datetime_safe.new_date(value)
390 return value.strftime(self.format)
391 return value
108@register.filter
109@stringfilter
110def parse_date(value):
111 return datetime.strptime(value, '%Y-%m-%dT%H:%M:%S+0000') # 2011-07-13T10:24:18+0000

Related snippets