使用datetime这段代码有什么问题?

时间:2018-02-01 16:07:18

标签: python datetime

contract MyToken {
    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;

    /* Initializes contract with initial supply tokens to the creator of the contract */
    function MyToken(
        uint256 initialSupply
        ) {
        balanceOf[msg.sender] = initialSupply;              // Give the creator all initial tokens
    }

    /* Send coins */
    function transfer(address _to, uint256 _value) {
        require(balanceOf[msg.sender] >= _value);           // Check if the sender has enough
        require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
        balanceOf[msg.sender] -= _value;                    // Subtract from the sender
        balanceOf[_to] += _value;                           // Add the same to the recipient
    }
}

它给了我这个错误信息:

  

TypeError:不支持的操作数类型 - :' datetime.date'和   ' STR'

我试图将它们转换为没有结果。

2 个答案:

答案 0 :(得分:0)

您的问题是从datestr的类型转换,反之亦然:

import datetime

CurrentDate = datetime.date.today()
print(CurrentDate.strftime('%m/%d/%Y'))

UserInput =  input("When is your birthday? (mm/dd/yyyy) ")
birthday = datetime.datetime.strptime(UserInput, '%m/%d/%Y').date()
days = birthday - CurrentDate
print("your next birthday is in " + str(days))

答案 1 :(得分:0)

正如bernie所说,CurrentDatestring

import datetime

CurrentDate = datetime.date.today().strftime('%m/%d/%Y') 
print(CurrentDate)

UserInput =  input("When is your birthday? (mm/dd/yyyy) ")
birthday = datetime.datetime.strptime(UserInput, '%m/%d/%Y').date()
current_date = datetime.datetime.strptime(CurrentDate, '%m/%d/%Y').date()
print(birthday)

days = (birthday - current_date
print("your next birthday is in " + str(days) )

您的算法可能仍需要一些工作:)

02/01/2018
When is your birthday? (mm/dd/yyyy) 01/01/2018
2018-01-01
your next birthday is in -31 days, 0:00:00

将调整后的时差除以天数看起来更清晰:

days = (birthday - current_date)/datetime.timedelta(days=1)
print("your next birthday is in " + str(days) + " days")