I got this, but how to insert a date into it, the date i want is one month from current time
print('''
{} will play football on Y
'''.format(name1))
whereby the Y is one month later from the current time
I got this, but how to insert a date into it, the date i want is one month from current time
print('''
{} will play football on Y
'''.format(name1))
whereby the Y is one month later from the current time
Hint: To get the current month of the year, use the datetime
module:
>>> import datetime
>>> datetime.datetime.now().month
8
Where datetime.datetime.now()
gets the current time as a datetime
object, and the .month
attribute prints the current month (as a number)
from datetime import *
monthNames=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
name1 = str(input("Please input your name: "))
thisMonth = int(datetime.now().month)
thisMonth_name = monthNames[thisMonth]
print("%s will play football on %s" %(name1, thisMonth_name))
OR
import datetime as dt
import calendar
name1 = str(input("Please input your name: "))
thisMonth = int(dt.datetime.now().month)+1
monthName = str(calendar.month_name[thisMonth])
print("%s will play football on %s" %(name1, monthName))
Also see: Get month name from number
https://docs.python.org/3/library/datetime.html
https://docs.python.org/3/library/calendar.html#calendar.month_name
If you want to print datetime one month later from current time,
you can use datetime
and dateutil
in combination.
from datetime import date, datetime
from dateutil import relativedelta
today = datetime.today()
monthplus1 = today + relativedelta.relativedelta(months=1)
print today
print monthplus1
# output
> datetime.datetime(2017, 8, 31, 8, 52, 3, 75585)
> datetime.datetime(2017, 9, 30, 8, 52, 3, 75585)
then you can use the datetime attributes like monthplus1.month
to get the next month.
and if you want names of the month, use a list like in below answer
monthNames=["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
with little tweak, so the 0th will be empty and you can get the Month Names
monthNames[monthplus1.month]
> 'September'
Then use these values as you would normaly do using format
print('''
{} will play football on {}
'''.format(name1, monthNames[monthplus1.month]))