time1 = datetime(2020,8,17)
time1
datetime.datetime(2020, 8, 17, 0, 0)
now = datetime.now()
now
datetime.datetime(2020, 8, 17, 15, 42, 27, 280357)
time.time()
1597650184.4910653
now.strftime('%Y-%m-%d %H:%M:%S %A %j')
'2020-08-17 15:42:27 Monday 230'
now.date()
datetime.date(2020, 8, 17)
now.time()
datetime.time(15, 42, 27, 280357)
time2 = datetime(2020,6,9)
time2
datetime.datetime(2020, 6, 9, 0, 0)
datetime.datetime(2020, 8, 17, 15, 42, 27, 280357)
time1-time2
datetime.timedelta(days=69)
time_str = '北京时间:2020-08-08 09:10:20'
datetime.strptime(time_str,'北京时间:%Y-%m-%d %H:%M:%S')
datetime.datetime(2020, 8, 8, 9, 10, 20)
timelist = Series(
[f'北京时间:2020-6-{9+i} 09:01:02' for i in range(5)]+\
[f'时间:2020-7-{1+i} 09:01:02' for i in range(5)]+\
[f'本地时间:2020-7-{6+i} 09:01:02' for i in range(5)]
)
timelist
0 北京时间:2020-6-9 09:01:02
1 北京时间:2020-6-10 09:01:02
2 北京时间:2020-6-11 09:01:02
3 北京时间:2020-6-12 09:01:02
4 北京时间:2020-6-13 09:01:02
5 时间:2020-7-1 09:01:02
6 时间:2020-7-2 09:01:02
7 时间:2020-7-3 09:01:02
8 时间:2020-7-4 09:01:02
9 时间:2020-7-5 09:01:02
10 本地时间:2020-7-6 09:01:02
11 本地时间:2020-7-7 09:01:02
12 本地时间:2020-7-8 09:01:02
13 本地时间:2020-7-9 09:01:02
14 本地时间:2020-7-10 09:01:02
dtype: object
timelist.apply(lambda x:x[x.find(':')+1:])
0 2020-6-9 09:01:02
1 2020-6-10 09:01:02
2 2020-6-11 09:01:02
3 2020-6-12 09:01:02
4 2020-6-13 09:01:02
5 2020-7-1 09:01:02
6 2020-7-2 09:01:02
7 2020-7-3 09:01:02
8 2020-7-4 09:01:02
9 2020-7-5 09:01:02
10 2020-7-6 09:01:02
11 2020-7-7 09:01:02
12 2020-7-8 09:01:02
13 2020-7-9 09:01:02
14 2020-7-10 09:01:02
dtype: object
timelist.apply(lambda x:datetime.strptime(x[x.find(':')+1:],'%Y-%m-%d %H:%M:%S'))
0 2020-06-09 09:01:02
1 2020-06-10 09:01:02
2 2020-06-11 09:01:02
3 2020-06-12 09:01:02
4 2020-06-13 09:01:02
5 2020-07-01 09:01:02
6 2020-07-02 09:01:02
7 2020-07-03 09:01:02
8 2020-07-04 09:01:02
9 2020-07-05 09:01:02
10 2020-07-06 09:01:02
11 2020-07-07 09:01:02
12 2020-07-08 09:01:02
13 2020-07-09 09:01:02
14 2020-07-10 09:01:02
dtype: datetime64[ns]
def getTime(x):
time_str = x.split(':',1)[1]
return datetime.strptime(time_str,'%Y-%m-%d %H:%M:%S')
timelist.apply(getTime)
0 2020-06-09 09:01:02
1 2020-06-10 09:01:02
2 2020-06-11 09:01:02
3 2020-06-12 09:01:02
4 2020-06-13 09:01:02
5 2020-07-01 09:01:02
6 2020-07-02 09:01:02
7 2020-07-03 09:01:02
8 2020-07-04 09:01:02
9 2020-07-05 09:01:02
10 2020-07-06 09:01:02
11 2020-07-07 09:01:02
12 2020-07-08 09:01:02
13 2020-07-09 09:01:02
14 2020-07-10 09:01:02
dtype: datetime64[ns]
from dateutil.parser import parse
parse('2020-8-17 16:30')
datetime.datetime(2020, 8, 17, 16, 30)
parse('2020-8-17 4:32 pm')
datetime.datetime(2020, 8, 17, 16, 32)
parse('Aug 17,2020 4:30:20 pm')
datetime.datetime(2020, 8, 17, 16, 30, 20)
parse('2020 Aug 17 4:30:20 pm')
datetime.datetime(2020, 8, 17, 16, 30, 20)
parse('2020 8 17')
datetime.datetime(2020, 8, 17, 0, 0)
parse('2020.8.17')
datetime.datetime(2020, 8, 17, 0, 0)
parse('2020/8/17')
datetime.datetime(2020, 8, 17, 0, 0)
parse('6-12-2020',dayfirst=True)
datetime.datetime(2020, 12, 6, 0, 0)
parse('6-12-2020',dayfirst=False)
datetime.datetime(2020, 6, 12, 0, 0)
pd.to_datetime()
pd.date_range()
datelist = ['2020/8/16','2020/8/17']
pd.to_datetime(datelist)
DatetimeIndex(['2020-08-16', '2020-08-17'], dtype='datetime64[ns]', freq=None)
datelist2 = ['2020/8/16','2020/8/17', NA , None]
pd.to_datetime(datelist2)
DatetimeIndex(['2020-08-16', '2020-08-17', 'NaT', 'NaT'], dtype='datetime64[ns]', freq=None)