In [4]:
d1 = '2018-01-31'
year, month, day = d1.split('-')
print(year)
print(month)
print(day)
In [6]:
import datetime
myDate = datetime.datetime.strptime(d1, "%Y-%m-%d")
myDate
Out[6]:
In [9]:
print(myDate.day, "\n", myDate.month, "\n", myDate.year)
In [1]:
## this example comes from some of my code
# It successfully generates the kind of now() I wanted and uses EST on my computer
# based on how it is written, it should use Pacific or other timezones if run on a computer
# in those time zones:
# note: gmtime() produced results in Grenwich Mean Time
# localtime() seems to get the local time from the computer (in my case EST)
from time import localtime, strftime
def getNow():
return strftime("%Y-%m-%d %H:%M:%S", localtime())
In [2]:
getNow()
Out[2]:
Content taken from here.
In [5]:
>>> from datetime import datetime
>>> str(datetime.now())
Out[5]:
In [4]:
>>> from time import gmtime, strftime
>>> strftime("%Y-%m-%d %H:%M:%S", gmtime())
Out[4]:
In [3]:
>>> from datetime import datetime
>>> datetime.now().strftime('%Y-%m-%d %H:%M:%S')
Out[3]:
In [6]:
>>> import datetime
>>> datetime.datetime.now()
Out[6]:
In [8]:
datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)
Out[8]:
In [9]:
from datetime import datetime
datetime.now().time()
Out[9]:
In [11]:
# combining some of the above:
datetime.now().time().strftime('%H:%M:%S')
Out[11]:
In [12]:
datetime.now().date().strftime('%Y-%m-%d')
Out[12]:
In [ ]: