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"))
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 10 months ago.
Improve this question
already clean a lot of information into this, and i'm feel stuck now, if anyone can give some ideas and how to proceed i will apreacite so much.
later i need to join with this:
and timezone abbr. ¿there's a library or something that can handle datetypes like this?
my desired output is something like this: 2022-05-04 18:00:00 but i have this:
[]
As mentioned in the comment, you should make sure your dates are strings in order to concatenate them. You can do this like that:
df.column_name = df.column_name.astype(str)
After that you can use python's datetime library to format it as dates.
date = 'May-02-2022'
time = '9:00 AM'
timestamp = a + ' ' + b
timestamp = pd.to_datetime(timestamp, format='%b-%d-%Y %I:%M %p')
print(timstamp)
>>> Timestamp('2022-05-02 09:00:00')
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 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.
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)