Date format in models - django-models

Hello how do i change the date format in models
for Eg: i have
date = models.DateField(blank=True, null=True)
by default it yyyy-mm-dd format how do i change it to dd-mm-yyyy format

A date is a date, and doesn't have a "format". A format applies when you represent the date as a string, or when you wish to interpret a string as a date.

You could play around with DATE_INPUT_FORMATS and similar settings for your project.
You define the setting in the settings.py file for your project. For instance:
DATE_INPUT_FORMATS = ('%d-%m-%Y', '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', '%b %d %Y',
'%b %d, %Y', '%d %b %Y', '%d %b, %Y', '%B %d %Y',
'%B %d, %Y', '%d %B %Y', '%d %B, %Y')

You're better off worrying about the format on output, not how it is stored. A DateField is stored as your database's native column of the same type.
If you really want this, you can set DATE_FORMAT in your settings.py, like so:
DATE_FORMAT='d-m-Y'
This makes a default display like this:
Feb 5, 2010
Look like this:
05-02-2010
Now of course there are other places where this will be overridden, such as the admin app and default form widgets, so it's not like this is a total solution to all your problems. Just get used to using datetime.datetime.strftime() like so:
>>> # using one of my own apps as an example where 'created' is a DateTimeField
>>> r.created
datetime.datetime(2010, 1, 28, 10, 5, 19, 38963)
>>> print r.created # default output (this is a Python default)
2010-01-28 10:05:19.038963
>>> print datetime.datetime.strftime(r.created, "%m-%d-%Y") # forcing it to m-d-Y format
01-28-2010

Related

Formatting logic app expression for DateTime

I am generating a file for export with the file name utcnow in the logic app expression which returns a value like this
utcNow()- "2020-06-01T15:41:15.5103915Z"
I want to convert it like "20200601154151" that means I need to remove some characters like" -","T" and millisecondsfollwed by Z,
I tried few combination of string format and I am not getting it right hoping you guys to help me.
Thanks,
There are many options for custom date formats. Here is a simple guide:
yyyy = Year (2020)
MM = Month (06)
dd = Day (01)
HH = Hour (15)
mm = Minute (41)
ss = Second (15)
Construct a format string (ex: yyyyMMddHHmmss) based on your requirements and pass it to formatDateTime:
formatDateTime(utcNow(), 'yyyyMMddHHmmss')
The resulting value will be '20200601154115'. There are many additional options at the link above.

Display Month in Spanish using strftime?

I am trying to get the format a date from 2018-09-01 to September 1, 2018.
I have been using:
strftime(displayDate, sizeof(displayDate), "%B %d %Y", &date_obj);
It works perfectly, but, is there a way to format the Month to be in Spanish?
The strftime function formats dates based on the current locale. So you'll need to set the locale for LC_TIME first:
setlocale(LC_TIME, "es_ES-UTF_8");
See setlocale for more information.

Angular localization problems with number and date format

I am trying to format a date based on localisation and have the locale files setup correct so when using date format of shortDate I get the difference between UK and US format.
However we need the date to show the full year 2016 and not 16.
If I code it as dd MM yyyy then that gives me the correct UK format but when toggling to US mode the filter keeps it in that dd MM yyyy format.
How can I enable the date to be of type day month year for UK and month day year for US etc ?
Try https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
// US English uses month-day-year order
console.log(date.toLocaleDateString('en-US'));
// → "12/19/2012"
// British English uses day-month-year order
console.log(date.toLocaleDateString('en-GB'));
// → "20/12/2012"
Note this is not IE friendly like most of html5 and ES6 :v.
other way is to use http://momentjs.com/

In Joomla using JHTML::calendar, is there a way to output Day, mm,dd?

It's pretty straight forward to use
echo JHTML::calendar('', 'date', 'dob', '%Y-%m-%d'); to get the date as 2014-07-01. Is there a way to get Tue 07-01? I don't need the year. I can't find any reference to getting the actual day of the week.
The bit you need to update is %Y-%m-%d - This is formatting the date.
I think it is using this system : http://ellislab.com/expressionengine/user-guide/templates/date_variable_formatting.html and so %D %m-%d should work, though I haven't had chance to test that.
edit: %a seems to work instead of %D for the day

CakePHP date 5 days from now using CakeTime

I need to create a date to be stored in mysql. The date is 5 days from now. I thought maybe CakeTime::dayAsSql('+5 days') but I'm missing a parameter which the documentation doesn't describes and also I dont really know if this will give me what I need. Any ideas?
Have you read the documentation at all? I'm asking because CakeTime::dayAsSql() is clearly not what you want:
Returns a partial SQL string to search for all records between two
times occurring on the same day.
Use CakeTime::format():
echo CakeTime::format('+5 days', '%B %e, %Y %H:%M %p');

Resources