Extract Time from Date-time field

user21945429

I need to extract the time from Date-time field, example below. Then print the Date and time separately.

sample_date = "Fri, 1 Nov 2002 01:45:04 +0100"

I assume I will have to separate the date and time first then print both segments, but not sure where to begin.

chickity china chinese chicken

Literally splitting into both 'date' and 'time' segments of a datetime object:

import datetime

s = "Fri, 1 Nov 2002 01:45:04 +0100"
dt = datetime.datetime.strptime(s, "%a, %d %b %Y %H:%M:%S %z")

Then you can extract as you wish from the available parts of a datetime object, e.g.

date = dt.date()
time = dt.time()

>>> print(date)
>>> print(time)

2002-11-01
01:45:04

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related