-1

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

TerryA
  • 58,805
  • 11
  • 114
  • 143

4 Answers4

2

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)

TerryA
  • 58,805
  • 11
  • 114
  • 143
1
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

S.Ahmed
  • 191
  • 1
  • 4
  • 12
  • Adding 1 to the month won't work in December. You can do `thisMonth %= 12`. – Paulo Almeida Aug 31 '17 at 03:00
  • @PauloAlmeida, can you please give the full syntax of the code where I can do `thisMonth%=12` so that this code works in December as well? – S.Ahmed Aug 31 '17 at 06:04
  • Put that exact line, `thisMonth %= 12`, just below `thisMonth = int(dt.datetime.now().month)+1`. To be clear, in case you're not familiar with the operation, that is equivalent to `thisMonth = thisMonth % 12`, which, in December, divides 13 by 12 and returns the remainder, 1. – Paulo Almeida Aug 31 '17 at 06:46
0

Often the easiest way of manipulating dates and times in ways like this is to use the arrow library. In this case, you can get what you want with this code:

>>> import arrow
>>> arrow.now().shift(months=+1).format('MMMM')
'September'
Bill Bell
  • 21,021
  • 5
  • 43
  • 58
0

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]))
Bijoy
  • 1,131
  • 1
  • 12
  • 23