This question already has answers here:
Getting today's date in YYYY-MM-DD in Python?
(12 answers)
Closed 2 years ago.
How do I print the current date in the below format?
year/month/date
I have been using the below, but it is adding an extra space. I need it to look like this '2020/9/14' and not like '2020 / 9 / 14. Any ideas? Below is current code for the latter option.
str(todays_date.year)
str(todays_date.month)
str(todays_date.day)
dash = "/"
str(dash)
print(todays_date.year,dash,todays_date.month,dash,todays_date.day)
Like this:
from datetime import datetime
print(datetime.today().strftime('%Y/%-m/%-d'))
UPDATE: I added the hyphens in there, realizing that you didn't want the leading zero on a one-digit month or day.
Today's result:
2020/9/14
If you want to use the individual values from the date and do your own formatting, you can do this to get the same result:
print("{}{}{}{}{}".format(todays_date.year,dash,todays_date.month,dash,todays_date.day))
this:
date = '2020 / 09 /14'
print(date.replace(' ',''))
prints:
'2020/09/14'
Related
This question already has answers here:
Formatting timedelta objects [duplicate]
(7 answers)
Closed 5 months ago.
I want to generate a timestamp only in hour and minutes format, like this:
154h 27m
Please check my logic below
planned_downtime = timedelta(hours=random.randrange(150))
However, the result is coming in seconds. To convert it to above mentioned format, I applied this:
planned_downtime.strftime("%H %M")
But I'm getting errors. How can I convert this randomly generated time in above format?
Like this maybe:
m = random.randint(0,60)
h = random.randint(0,24)
ts = f'{h}h {m}m'
print(ts)
15h 48m
This question already has answers here:
How to get all objects with a date that fall in a specific month SQLAlchemy
(3 answers)
Closed 5 months ago.
My current code is looking like this
start_month_june= dt.date(2012 ,6, 1)
end_month_june = dt.date(2012 ,6, 30)
june_temps = session.query(Measurement.date, Measurement.tobs).filter(Measurement.date >= start_month_june, Measurement.date <= end_month_june)
This code currently selects all the dates between June 1 and June 30th for 2012. How can I filter it for the month of June for all the years in my dataset (2010-2018) instead?
This seems to work in sqlite but does not work with (at least) postgres.
It extracts the month as a string from the date. The months are 0 padded, meaning they look like '01..06..12', so for June we use '06'.
from sqlalchemy.sql import func
session.query(Measurement.date, Measurement.tobs).filter(func.strftime('%m', Measurement.date) == '06').all()
sqlalchemy's func docs
sqlite's strftime docs
This question already has answers here:
datetime from string in Python, best-guessing string format
(4 answers)
How can I parse multiple (unknown) date formats in python?
(4 answers)
How can I translate dates and times from natural language to datetime? [closed]
(2 answers)
How to format date string via multiple formats in python
(5 answers)
Closed 2 years ago.
I have a problem in Python 3.9 64x bit.
In a program I am writing, I need to be able to convert any inputted date into the format %d%m%y.
For example, if the user entered 12 December 2021, the program will convert it to 121221, and if the user enters 2021 12 December, it will still convert it to 121221.
You could use pandas to_datetime and then strftime.
from pandas import to_datetime
to_datetime('12 December 2021').strftime('%d%m%y') ## returns 121221
to_datetime('2021 12 December').strftime('%d%m%y') ## returns 121221
pandas tries to infer the format when parsing the string.
Note, without specifying the datetime format for the string entered by the user there is of course ambiguity. E.g. what is meant by a the string '11/12/2021'. It could be '11 December 2021' or '12 November 2021'.
this of course is error prone if the user enters
>>> from_date="12 December 2021"
>>> import time
>>> conv=time.strptime(from_date,"%d %B %Y")
>>> time.strftime("%d/%m/%y",conv)
'121221'
>>> from_date="2021 12 December"
>>> import time
>>> conv=time.strptime(from_date,"%Y %d %B")
>>> time.strftime("%d%m%y",conv)
'121221'
This question already has answers here:
Parse date string and change format
(10 answers)
How to convert a date string to different format [duplicate]
(2 answers)
Closed 3 years ago.
I need help creating a function that takes a user input(Numeric Date) and turns it into a string
For Example:
input: 2018-06-20
output: June 20th 2018
any hints or code would help me out. Thank you
import datetime
d = '2018-06-20'
datetime.datetime.strptime(d, '%Y-%m-%d').strftime('%B %d %Y')
Output:
June 20 2018
For more formatting you can look here
This question already has answers here:
Parse date string and change format
(10 answers)
Closed 7 years ago.
I need to convert date string "2013-1-25" to string "1/25/13" in python.
I looked at the datetime.strptime but still can't find a way for this.
I assume I have import datetime before running each of the lines of code below
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
prints "01/25/13".
If you can't live with the leading zero, try this:
dt = datetime.datetime.strptime("2013-1-25", '%Y-%m-%d')
print '{0}/{1}/{2:02}'.format(dt.month, dt.day, dt.year % 100)
This prints "1/25/13".
EDIT: This may not work on every platform:
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
If you can live with 01 for January instead of 1, then try...
d = datetime.datetime.strptime("2013-1-25", '%Y-%m-%d')
print datetime.date.strftime(d, "%m/%d/%y")
You can check the docs for other formatting directives.