2016-03-18 33 views
8

今から明日の12:00までの秒数を計算します。だから私は明日12時datetimeオブジェクトを取得する必要があります。Pythonでdatetime.datetime.hourを変更するには?

これは擬似コードです:

AttributeError: attribute 'hour' of 'datetime.datetime' objects is not writable 

がどのように時間を変更することができるか、どのように私は明日12時datetimeオブジェクトを取得することができます:それは、このエラーが発生します

today_time = datetime.datetime.now() 
tomorrow = today_time + datetime.timedelta(days = 1) 
tomorrow.hour = 12 
result = (tomorrow-today_time).total_seconds() 

しかし?

+0

あなたは[この回答](HTTPを見てみることができ://stackoverflow.com/questions/8777753/converting-datetime-date-to-utc-timestamp-in-python)ので、**明日の12:00 ** datetimeをタイムスタンプに変換して、 'time.time( ) ' – pixis

答えて

18

既存の1に基づいて、新しいdatetimeオブジェクトを生成するreplace methodを使用します。

tomorrow = tomorrow.replace(hour=12) 

Return a datetime with the same attributes, except for those attributes given new values by whichever keyword arguments are specified. Note that tzinfo=None can be specified to create a naive datetime from an aware datetime with no conversion of date and time data.

2

これを試してみてください:

tomorrow = datetime.datetime(tomorrow.year, tomorrow.month, tomorrow.day, 12, 0, 0) 
関連する問題