如何在python中将strftime或字符串格式转换为timestamp/Date?
How to convert strftime or string format to timestamp/Date in python?
我对 Python 编码还很陌生。我试图获取一个月的开始和结束日期,然后将其与同一 excel.
中的另一个日期列进行比较
我只需要 mm/dd/yy
格式的日期,不需要时间。
final_month_end_date
基本上是一种字符串格式,我将其与实际日期进行比较,但它给我一个错误提示
"TypeError: Cannot compare type 'Timestamp' with type 'str'"
我也试过.timestamp()
功能,但没用。
我该如何解决这个问题?
import datetime as dt
import strftime
now1 = dt.datetime.now()
current_month= now1.month
current_year= now1.year
month_start_date= dt.datetime.today().strftime("%Y/%m/01")
month_end_date= calendar.monthrange(current_year,current_month)[1]
final_month_end_date= dt.datetime.today().strftime("%Y/%m/"+month_end_date)
要将字符串转换为 DateTime 对象,请使用 datetime.strptime. Once you have the datetime object, convert it to a unix timestamp using time.mktime。
import time
import datetime as dt
from time import mktime
from datetime import datetime
now1 = dt.datetime.now()
current_month= now1.month
current_year= now1.year
month_start_date= dt.datetime.today().strftime("%Y/%m/01")
month_end_date= "30"
final_month_end_date= dt.datetime.today().strftime("%Y/%m/"+month_end_date)
# Use datetime.strptime to convert from string to datetime
month_start = datetime.strptime(month_start_date, "%Y/%m/%d")
month_end = datetime.strptime(final_month_end_date, "%Y/%m/%d")
# Use time.mktime to convert datetime to timestamp
timestamp_start = time.mktime(month_start.timetuple())
timestamp_end = time.mktime(month_end.timetuple())
# Let's print the time stamps
print "Start timestamp: {0}".format(timestamp_start)
print "End timestamp: {0}".format(timestamp_end)
我对 Python 编码还很陌生。我试图获取一个月的开始和结束日期,然后将其与同一 excel.
中的另一个日期列进行比较我只需要 mm/dd/yy
格式的日期,不需要时间。
final_month_end_date
基本上是一种字符串格式,我将其与实际日期进行比较,但它给我一个错误提示
"TypeError: Cannot compare type 'Timestamp' with type 'str'"
我也试过.timestamp()
功能,但没用。
我该如何解决这个问题?
import datetime as dt
import strftime
now1 = dt.datetime.now()
current_month= now1.month
current_year= now1.year
month_start_date= dt.datetime.today().strftime("%Y/%m/01")
month_end_date= calendar.monthrange(current_year,current_month)[1]
final_month_end_date= dt.datetime.today().strftime("%Y/%m/"+month_end_date)
要将字符串转换为 DateTime 对象,请使用 datetime.strptime. Once you have the datetime object, convert it to a unix timestamp using time.mktime。
import time
import datetime as dt
from time import mktime
from datetime import datetime
now1 = dt.datetime.now()
current_month= now1.month
current_year= now1.year
month_start_date= dt.datetime.today().strftime("%Y/%m/01")
month_end_date= "30"
final_month_end_date= dt.datetime.today().strftime("%Y/%m/"+month_end_date)
# Use datetime.strptime to convert from string to datetime
month_start = datetime.strptime(month_start_date, "%Y/%m/%d")
month_end = datetime.strptime(final_month_end_date, "%Y/%m/%d")
# Use time.mktime to convert datetime to timestamp
timestamp_start = time.mktime(month_start.timetuple())
timestamp_end = time.mktime(month_end.timetuple())
# Let's print the time stamps
print "Start timestamp: {0}".format(timestamp_start)
print "End timestamp: {0}".format(timestamp_end)