In Python, in order to print the current date consisting of a year, month, and day, it has a module named datetime. From the DateTime module, import date class. Create an object of the date class. Call the today( ) function of date class to fetch todays date.
Use strftime() function of a datetime class The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.
import datetime
now = datetime.datetime.now()
print(now.year)
The above code works perfectly fine for me.
The following seems to work:
import datetime
print (datetime.datetime.now().strftime("%y"))
The datetime.data object that it wants is on the "left" of the dot rather than the right. You need an instance of the datetime to call the method on, which you get through now()
I use this which is standard for every time
import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))
This function allows you to get the date and time in lots of formats (see the bottom of this post).
# Get the current date or time
def getdatetime(timedateformat='complete'):
from datetime import datetime
timedateformat = timedateformat.lower()
if timedateformat == 'day':
return ((str(datetime.now())).split(' ')[0]).split('-')[2]
elif timedateformat == 'month':
return ((str(datetime.now())).split(' ')[0]).split('-')[1]
elif timedateformat == 'year':
return ((str(datetime.now())).split(' ')[0]).split('-')[0]
elif timedateformat == 'hour':
return (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[0]
elif timedateformat == 'minute':
return (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[1]
elif timedateformat == 'second':
return (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[2]
elif timedateformat == 'millisecond':
return (str(datetime.now())).split('.')[1]
elif timedateformat == 'yearmonthday':
return (str(datetime.now())).split(' ')[0]
elif timedateformat == 'daymonthyear':
return ((str(datetime.now())).split(' ')[0]).split('-')[2] + '-' + ((str(datetime.now())).split(' ')[0]).split('-')[1] + '-' + ((str(datetime.now())).split(' ')[0]).split('-')[0]
elif timedateformat == 'hourminutesecond':
return ((str(datetime.now())).split(' ')[1]).split('.')[0]
elif timedateformat == 'secondminutehour':
return (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[2] + ':' + (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[1] + ':' + (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[0]
elif timedateformat == 'complete':
return str(datetime.now())
elif timedateformat == 'datetime':
return (str(datetime.now())).split('.')[0]
elif timedateformat == 'timedate':
return ((str(datetime.now())).split('.')[0]).split(' ')[1] + ' ' + ((str(datetime.now())).split('.')[0]).split(' ')[0]
To obtain the time or date, just use getdatetime("<TYPE>")
, replacing <TYPE>
with one of the following arguments:
All example outputs use this model information: 25-11-2017 03:23:56.477017
Argument | Meaning | Example output |
---|---|---|
day | Get the current day | 25 |
month | Get the current month | 11 |
year | Get the current year | 2017 |
hour | Get the current hour | 03 |
minute | Get the current minute | 23 |
second | Get the current second | 56 |
millisecond | Get the current millisecond | 477017 |
yearmonthday | Get the year, month and day | 2017-11-25 |
daymonthyear | Get the day, month and year | 25-11-2017 |
hourminutesecond | Get the hour, minute and second | 03:23:56 |
secondminutehour | Get the second, minute and hour | 56:23:03 |
complete | Get the complete date and time | 2017-11-25 03:23:56.477017 |
datetime | Get the date and time | 2017-11-25 03:23:56 |
timedate | Get the time and date | 03:23:56 2017-11-25 |
I always use this code, which print the year to second in a tuple
import datetime
now = datetime.datetime.now()
time_now = (now.year, now.month, now.day, now.hour, now.minute, now.second)
print(time_now)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With