简体   繁体   中英

Extracting week number from timestamp in Python

I have timestamps in the format

          Sep 12 2019 21:28:11
1         Sep 12 2019 21:28:11
2         Oct 13 2019 21:28:11
3         Oct 14 2019 21:28:11
4         Nov 15 2019 21:28:11

I need to write a python script by which I can get the week number from the date mentioned. Can anyone help me with the same?

Use datetime module.

import datetime
datetime.datetime.strptime("Sep 12 2019 21:28:11", "%b %d %Y %H:%M:%S").strftime("%V")

Just another option may be isocalendar function https://docs.python.org/3/library/datetime.html#datetime.datetime.isocalendar

>>> import datetime
>>> datetime.datetime.strptime("Sep 12 2019 21:28:11", "%b %d %Y %H:%M:%S").isocalendar()
(2019, 37, 4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM