Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a vast number of files that need to be deleted past a certain date.
I can't use the datestamp of the file because the files are created some time before they are used. A file called OCT21.txt needs to be deleted a few days after October 21st, but it could have been created in May.
My question is: is it possible to convert the "OCT21" string into a format that the time module can use?
This is in the datetime docs
classmethod datetime.strptime(date_string, format)
Return a datetime corresponding to date_string, parsed according to format.
This is equivalent to datetime(*(time.strptime(date_string, format)[0:6])).
ValueError is raised if the date_string and format can’t be parsed by time.strptime() or if it returns a value which isn’t a time tuple.
For a complete list of formatting directives, see strftime() and strptime() Behavior.
You can give it a format (%b%d in your case) and it'll return the datetime.
Try this:
from datetime import datetime
target = datetime.now().strftime('%b%d').upper()
if filename == target + '.txt':
# Do your thing
It is possible, have a look at time.strptime.
import time
filedate = time.strptime("OCT21", "%b%d")
Of course, in the resulting struct_time neither the year nor exact time are valid.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a date with format in 'yyyy-dd-mm'. How do I convert to 'yyyy-mm-dd' format, been bit of a struggle with strftime and strptime!
Assuming the date is '1919-01-12', I want to see it in the format '1919-12-01'.
from datetime import datetime
date = '1919-01-12'
formatted_date = datetime.strptime(date, '%Y-%d-%m').strftime('%Y-%m-%d')
datetime.strptime(date, '%Y-%d-%m') creates a datetime object from a string, .strftime('%Y-%m-%d') formats the datetime as a string accoring to the specified pattern
try this code
import datetime
print (datetime.datetime.strptime("1919-01-12", "%Y-%d-%m").strftime("%Y-%m-%d"))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
import datetime
now = datetime.datetime.now()
print("Current date and time:")
print(now.strftime("%T-%m-%d %H:%M:%S"))
the now object created is why we have to use two times the datetime function
You are not using "two times the datetime function". In datetime.datetime.now(), the function is now. The first datetime is the package, and the second datetime is sub-package. The objects are grouped hierarchically for sake of better organization, for example alongside datetime.datetime there is also datetime.date (compare datetime.datetime.now() with datetime.date.today()).
If you do not want to use both levels each time, then instead of:
import datetime
you could do:
from datetime import datetime
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a column in a pandas dataframe filled with dates (in the format YYYY-MM-DD).
I would like to write a code that would automatically change the values from time from today. That is if the date is 07/08/2019 it would automatically change that into the number one (because in one year), and if the date was in 6 months it would change into 0.5 (years). I need it to be precise to the day exactly as a fraction of the year, so like we could get number with a few decimal.
Would you kindly know how to do that ?
Firstly you need to parse the date string into a date object using strptime
Then get the difference between that and now. Convert it into days and divide by a year
Something like:
import datetime
then = datetime.datetime.strptime('2019-09-07', "%Y-%m-%d").date()
now = datetime.datetime.date().now()
print (then - now).days / 365.0
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to import experimental time data which is not really a time, date, or datetime according to the python formalism. It is a time-elapsed format as Dd HH:MM:SS.m where the first time point would be 0d 00:00:00.00 and a point 26 hours later would be 1d 02:00:00.00.
Is there a way to use the datetime module to extract the appropriate time information without hard-coding a string search for the day and adding a multiple of 24 to the hour counter?
To convert your 'odd' format to datetime.timedelta object try:
from datetime import timedelta
import re
input = '1d 02:00:00.00'
def to_timedelta(input):
reg = re.search('([0-9])+d ([0-9]){2}:([0-9]){2}:([0-9]){2}\.([0-9]){2}', input)
ints = tuple(int(t) for t in reg.groups())
return timedelta(days=ints[0],
hours=ints[1],
minutes=ints[2],
seconds=ints[3],
milliseconds=ints[4])
td = to_timedelta(input)
print(td)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am converting the date to Day of the month.
when I am executing Ia m getting the following error.
File ".\date.py", line 8, in <module>
ans=datetime.date(year,month,date)
TypeError: an integer is required (got type str)
I don't understand this error. Return type is Str as expected.
I have not declared as int.
I am using Python3 and not getting why it is complaining about the datetype.
There's two issues.
First, to get the day of the month, you should use %d, not %A with strftime().
date = time.strftime("%d");
Second, this returns the day as a string, not as an integer, but datetime.date() requires its arguments to be integers, so you need to convert it with int(date)
date = int(time.strftime("%d"))
But there's really no need to use strftime() in the first place. If you have a date object, you can simply access its day property to get the day of the month.