How to validate date input between two dates in Python - python

I am trying to validate user input of date of birth. I have used datetime to validate that it's a valid existing date, however I am trying to make it so you can only enter dates between 1/1/1900 and the current date. I am using .today(), however I am very confused by the importing of datetime and date, and can't seem to get it working.
I also want to zero pad the dates so everything entered is in the format dd/mm/yyyy, either by forcing them to enter it like that, or by converting it to that format afterwads
Any help would be grately appreciated :)

Related

Trying to convert influxdb timestamp in a csv to date and time columns

I am migrating an InfluxDB database to mySQL. I have managed to export the influx data to a CSV file, which is great, but now I am stuck with the date and time field which has been given to me.
I have no idea what format it is in, after doing some research it tells me that it is in epoch time, but using python to try and convert the timestamp to an ISO format, it isn't recognised as a valid timestamp. Any idea how to get this converted. Ideally to separate date and time columns. The data that I have got is something like this :
time,absoluteHumidity
1578152602608558363,5.788981747966442
1578152608059500073,4.769760557208695
1578152613662193439,5.788981747966442
And the python that I was using to try and convert it, was this :
from datetime import datetime, timezone
print (datetime.fromtimestamp(1578152602608558363, timezone.utc))
Any help or suggestions would be appreciated !
According to the influxdb docs they store timestamp values with nanoseconds precision.
However the datetime.fromtimestamp method expects a floating point number and its integer part is in second precision.
So generally your approach is right you just need to divide the influx timestamp by 1e9 and it should just work:
from datetime import datetime, timezone
print(datetime.fromtimestamp(1578152602608558363 / 1e9, timezone.utc))

How to change date format (from yyyy-MM-DD to yyyy-MM)

I'm trying to change date format from yyyy-MM-dd to yyyy-MM.
Ultimately I want to be able to sum and group by month. So far the only working solution I found is adding concat(year(join_data["firstVisit"]), lit("-"), month(join_data["firstVisit"])).alias('firstVisitMonth') in my select statement but then it return the column as a string and I can't sort it correctly.
Try date_format:
date_format(join_data["firstVisit"], 'yyyy-MM')

Detect missing date in string using dateutil in python

I have a string which contains a timestamp. This timestamp may or may not contain the date it was recorded. If it does not I need to retrieve it from another source. For example:
if the string is
str='11:42:27.619' #It does not contain a date, just the time
when I use dateutil.parser.parse(str), it will return me a datetime object with today's date. How can I detect when there is no date? So I can get it from somewhere else?
I can not just test if it is today's date because the timestamp may be from today and I should use the date provided in the timestamp if it exists.
Thank you
What I would do is first check the string's length, if it contains it should be larger, then I would proceed as you mention.

If Date doesn't exist, get the next valid date

I am using Python's datetime lib to parse a date and create a datetime object from it.
datetime.datetime.strptime(request.GET['date'], '%d-%m-%Y').date()
This works as long as a valid date is passed. I could use a try and except case to handle invalid dates, but would like to return the next valid date, as this is a Django view where a redirect would make sense.
How could I solve this?

save date in one format in database - Django [duplicate]

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.

Categories