I want to save date in my django model into desired format.
post_date = str(request.POST['date'])
I am getting date from front end is 12/21/2016
i want to save this date in my django modal is like 21-Dec-2016.
How can i do it.
it will be good if you can provide some sample code.
Thanks in advance.
you want to store in DB string parameter? The easiest way convert input string to date and then convert from date to string. But of course it is more logical to store date format imho
Related
I want to create general custom field to read str dates and convert it to date with passed format.
So, I want something like this:
date_field = CustomDateField(format=custom_format)
Is it possible? I hope so
I have more than 1 Million records in excel file. I want to query on the Table using python, but date format is dd/mm/yyyy. I know that in MySQL the supported format is yyyy-mm-dd. I am restricted towards changing the format of date. Is there any possibility that I could do it on run-time. Just query on yyyy from dd/mm/yyyy and fetch the record.
How Do I query on such format only on Year and not on Month or Date to get data ?
Assuming the "date" is being received as a string, then RIGHT(date, 4) will give you just the year.
(I see no need to reformat the string if you only need the data. Otherwise see STR_TO_DATE()
I am working on a form and have a date field.
I want to save different date format for date field instead django used.
I am getting "01-jan-2016" date and want to save as it is in my database.when i am trying to save this same format is raise an error
[u"'07-Dec-2016' value has an invalid date format. It must be in YYYY-MM-DD format."].
I know this type of question asked already but they do not solve my problem.
views.py
post_date = request.POST['date']
lead_obj = CustomerLeads.objects.create(posting_date = post_date)
my models.py
class Leads(models.Model):
customer_name = models.CharField(max_length=50,null=True, blank=True)
title = models.CharField(max_length=100, null=True, blank=True)
posting_date = models.DateField()
Mysql
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
Postgresql
Date and time input is accepted in almost any reasonable format,
including ISO 8601, SQL-compatible, traditional POSTGRES, and others.
For some formats, ordering of day, month, and year in date input is
ambiguous and there is support for specifying the expected ordering of
these fields. Set the DateStyle parameter to MDY to select
month-day-year interpretation, DMY to select day-month-year
interpretation, or YMD to select year-month-day interpretation.
The table at the link shows that postgresql accept the format that you are looking for. But how is it stored? Let's try an insert
INSERT INTO stackoverflow_heatwatchlist(next_date_from, next_date_to)
VALUES('1999 Jan 05','2001 Jun 06');
And when you select, what you get is '2001-06-06' and '1999-01-05'
SQlite
Here you can insert in any format you want and it will be saved exactly as you entered, that's because sqlite does not enforce any strict type checking. Types in sqlite are purely cosmetic. but django has a different opinion on the matter and will not let you divert too much from the standard formats.
SQL Server
A lot of flexibility in how the data is accepted for insert. Refer to the table at the link. But how is it stored? '1999-01-05'
In conclusion
How the date is stored in the table is totally irrelevent, all that matters is what formats are accepted for input and how it's displayed. And that's where django's and python's excellent date and time formats come into play.
If you are not happy with them, you have two choices. The first is to store dates and times as unix timestamps and do all the formatting yourself, all you need is a little math really - a fairly common practice.
The second is to use CharField for storing your data but this is not very usefull. Not only do you have to do all the formatting and input validation yourself, any calculation would involve a lot of string processing and casting.
Try to add '%d-%b-%Y' to DATE_INPUT_FORMATS in your project settings.
In your views.py try this:
from dateutil import parser
d = parser.parse("07-December-2016")
print d
d = d.strftime("%d-%b-%Y")
print d
Output:
2016-12-07 00:00:00
07-Dec-2016
It will handle various formats.
I need to change the date into the format MM/YY. The issue is I don't always don't what the date format is to begin with. It's my understanding that the code below would need to know what format the date was in first before I can change it is that correct?
newdate = datetime.datetime.strptime(str(mydate), '%Y/%m/%d').strftime('%m-%Y')
as a result I get the error:
time data '2020-09-30' does not match format '%Y/%m/%d' (
How to I convert my date to MM/YY?
I'm using Django 1.4.6
newdate = datetime.datetime.strptime(str(mydate), '%Y-%m-%d').strftime('%m/%Y')
This question already has answers here:
data format value changes in database
(6 answers)
Closed 9 years ago.
forms.py
INPUT_FORMAT = (
('%d/%m/%Y','%m/%d/%Y')
)
class ReportDatetimeForm(forms.ModelForm):
date = forms.DateField(input_formats = INPUT_FORMAT,
widget=forms.DateInput()
class Meta:
model = Report
fields = ['date']
This is my form to get the input as date with two different format.While giving input date in this format %m/%d/%Y' the date and month gets interchange while save in database.That affects in my application.So i want to save both input format in a single format namely yyyy-mm-dd,so what ever input format the user enter ,the given format should save in the above mentioned format.
I want to know how to write a custom function in forms.py to save the date in database in single format.
Thanks
The actual date that is stored in the database is irrelevant to the input text going in to the field. Dates are stored as date objects, not arbitrary strings.
The problem you're facing is that the two formats you list as valid produce different date objects, for the same input, some of the time. They are ambiguous.
If a user enters the string "01/02/1990" into your form field, what is the actual date? Is it the 1st of February, 1990; or is it the 2nd of January, 1990? In your particular implementation, the date chosen will be the first format: 1st Feb. It will check each date format, in order, and try to parse the date. If it was successful, then it creates the date and saves it. If it was not successful, it tries each successive format.
Either choose a single format, or multiple formats that can never be ambiguous. The use of a javascript calendar picker can be extremely useful, or even individual text inputs for the various parts of the date.
What you should do is only allow ISO8601 date format "YYYY-MM-DD" in your django form. From within the UI you can allow the user to enter in dates in any format they like, as long as you convert it (with javascript) to the "YYYY-MM-DD" format before sending to the server. But what you SHOULD do is have 3 separate text inputs - one each for day, month, and year.