Detect missing date in string using dateutil in python - 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.

Related

How to validate date input between two dates in 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 :)

What's the correct datetime format for this string date generated by python?

I have this date example '2022-08-30T11:53:52.204219' stored in database, when I get it from database it's type is string so I wanted to convert it to a date type by using this python code
datetime.strptime('2022-08-30T11:53:52.204219', "%Y-%m-%d'T'%H:%M:%S.%f")
I also tried this one
datetime.strptime('2022-08-30T11:53:52.204219', "yyyy-MM-dd'T'HH:mm:ssZ")
But I always get this error response 'time data '2022-08-30T11:53:52.204219' does not match format "%Y-%m-%d'T'%H:%M:%S.%f'
I need help to convert this string date to an actual date
As per comment:
from datetime import datetime
print(datetime.strptime('2022-08-30T11:53:52.204219', "%Y-%m-%dT%H:%M:%S.%f"))
Result:
2022-08-30 11:53:52.204219

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')

python efficient way to validate date

I have a large file with the date in %m%d%Y format i.e 12012013 for 12th jan 2013.
I have to perform 2 things:
1) validate the date
2) store it in a list in sorted chronological format
for validation:
try:
parsedDate = datetime.strptime(date, '%m%d%Y')
return parsedDate
except:
return None'
using DateTime take a lot of time to parse the date. Since the format is mmddyyyy, can I validate it without using datetime efficiently?
2) For chronological order: I dont want to convert it to datetime and then sort it, is there a way I can use string to sort it. I have check a lot of answers, but almost all of them assumes that you have a list and then sort it.
I want to insert it in a sorted format?
datetime module is pretty good, still if you want any other option you can validate as reg expression, check: match dates using python regular expressions.
To sort the date without converting to datetime. Just convert it into format yyyymmdd then do string sort or just create a empty list then append the string to the the correct position based on value greater or lesser.
Would request you to try it yourself :)
If the format is %m%d%Y, the most efficient is using a RegEx (there are some profiling about that).
For instance:
import re
import datetime
match_date = re.compile(r'(\d{2})(\d{2})(\d{4})$').match
text = '12012013'
mo = match_date(text)
if mo:
date = datetime.date(int(mo.group(3)), int(mo.group(1)), int(mo.group(2)))
print(date)
# -> 2013-12-01
That way, the RegEx will do the first level of filtering and the date constructor the second (with an exception). Of course, you can improve your RegEx, this one is trivial for the demo.
If you know in advance that your dates are all valid you can avoid the conversion to date and use the tuple (year, month, day) for sorting, instead of using date.

storing date into mongodb using python in ISO format

I am trying to store date into mongodb using python(bottle framework).
I want to store it in the below format:
ISODate("2015-06-08 03:38:28")
Currently I am using the following command:
datetime.strptime(DateField, '%m/%d/%Y %H:%M:%S %p')
it is getting stored like this:
ISODate("2015-06-08T03:38:28Z")
How to store it without "T" and "Z" in it??
You are confusing how something is stored vs. how something is displayed.
In MongoDB, dates are stored as 64 bit integers, what you are seeing is the way it is represented so that we can easily determine what date and time the 64bit number represents.
The ISODate is just a helper method, which formats the date in the ISO date format.
So when you pass it in a normal date and time string, it will convert it into the correct format.
The format adds the T (to separate the time part) and the Z (as you have not identified a time zone, it is defaulted to UTC).
In short - you are not storing it with the T and the Z, that's just how it is displayed back to you.

Categories