簡體   English   中英

Python:將python對象date-time的格式更改為string,然后將其更改回python對象以與其他日期比較嗎?

[英]Python: Change the format of python object date-time to string and change it back to python object to be comparable with other date?

我有一個代碼,其中有兩個日期輸入(格式不同),並且希望它們具有相同的格式(日期時間python對象),因此我可以比較這兩天。 其中一個是字符串,另一個是python對象:

import pythonwhois
import datetime
from datetime import date
from dateutil.parser import parse
from datetime import datetime

bl_time = l.split('\t')
bl_time = bl_time.strip('\n')
bl_date = datetime.datetime.strptime(bl_time ,"%b %d %Y").strftime('%d/%m/%Y')
date1 = datetime.strptime(bl_date,'%d/%m/%Y')
w = pythonwhois.get_whois(domain)
date2 = (w['creation_date'])[0].strftime(bl_date, '%d/%m/%Y').strptime(bl_date,'%d/%m/%Y')

我收到此錯誤:

Traceback (most recent call last):
  File "/Users/Documents/scripts/whois.py", line 29, in <module>
    bl = datetime.datetime.strptime(bl_time ,"%b %d %Y").strftime('%d/%m/%Y')
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

有人可以幫忙嗎?

由於您是from datetime import datetime ,因此在您的代碼中, datetime ,而不是module 更改

datetime.datetime.strptime(bl_time, ...).strftime(...)` 

datetime.strptime(bl_time, ...).strftime(...)` 

並且您應該被設置為1

1至少就此錯誤而言... ;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM