草庐IT

datetime2

全部标签

Python datetime.utcnow() 返回不正确的日期时间

datetime.utcnow()此调用返回的日期时间不正确,比UTC/GMT延迟1小时(登记:http://www.worldtimeserver.com/current_time_in_UTC.asp)。它是否按应有的方式工作?例如,它现在正在返回:2015-02-1723:58:44.761000.当前UTC时间是:00:58,而不是23:58 最佳答案 我知道我迟到了五年,但今晚我遇到了同样的问题。根据我的经验,问题的解决方案是使用感知的UTC日期时间:utc_dt_aware=datetime.datetime.now(d

python - 使用 datetime 数据类型切片 pandas multiindex

我是pandas(ver0.14.0)的新手,遇到了以下问题:我正在尝试使用多索引对pandas数据帧进行切片。索引包含时间戳。如果仅使用日期作为时间戳进行切片,则效果很好。使用时间戳中的时间切片时,它不返回任何内容或返回异常。分割包含日期和时间的时间戳的正确方法是什么?更新:切片时间戳和其他索引和列的正确方法是什么?这是我的代码:dates=pd.DatetimeIndex([datetime.datetime(2012,1,1,12,12,12)+datetime.timedelta(days=i)foriinrange(6)])freq=[1,2]iterables=[dates

python - 无法填充 NumPy datetime64 数组

我正在尝试创建一个NumPy数组,该数组随后将由一些日期时间值填充。我似乎无法让它发挥作用。importnumpyasnpt=np.empty(3,dtype='datetime64')t我收到一个TypeError:Invaliddatetimeunit"generic"inmetadata。如果我尝试也一样:importnumpyasnpt=np.empty(3,dtype='datetime64')t[0]=np.datetime64('2014-12-1220:20:20')我得到:TypeError:Cannotcastnumpytimedelta64scalarfromme

python - datetime.strptime() 为 %Z 接受哪些可能的值?

Python的datetime.strptime()被记录为支持%Z字段中的时区。所以,例如:In[1]:datetime.strptime('2009-08-1914:20:36UTC',"%Y-%m-%d%H:%M:%S%Z")Out[1]:datetime.datetime(2009,8,19,14,20,36)但是,“UTC”似乎是我可以让它支持的唯一时区:In[2]:datetime.strptime('2009-08-1914:20:36EDT',"%Y-%m-%d%H:%M:%S%Z")ValueError:timedata'2009-08-1914:20:36EDT'd

python - 为什么 python 的 datetime.datetime.strptime ('201412' , '%Y%m%d' ) 不引发 ValueError?

在我给出的格式中,日期2014-01-02将由“20140102”表示。这是使用标准strptime正确解析的:>>>datetime.datetime.strptime("20140102","%Y%m%d")datetime.datetime(2014,1,2,0,0)在这种格式中,“201412”不是有效日期。docs假设“%m”指令是“以零填充的十进制数表示的月份”。它给出了示例“01、02、...、12”。days指令“%d”也应该用零填充。基于此,我预计“201412”将是这种格式的无效输入,因此会引发ValueError。相反,它被解释为2014-01-02:>>>dat

python - 格式化 datetime.utcnow() 时间

我已将时间保存为presentTime=datetime.datetime.utcnow()它的输出是2014-08-1821:11:35.537000。如何将其格式化为:2014年8月18日-21:11:35? 最佳答案 datetime.utcnow返回datetime目的。您可以使用它的strftime将其转换为所需格式的字符串的方法:>>>datetime.datetime.utcnow().strftime('%B%d%Y-%H:%M:%S')'August202014-13:55:49'

Python 将秒数转换为 datetime 日期时间

这个问题在这里已经有了答案:Howtoconvertintegertimestampintoadatetime(3个答案)关闭5年前。如何转换像1485714600这样的int,使我的结果最终成为Monday,January30,201712:00:00AM?我试过使用datetime.datetime但它给我的结果类似于“5天,13:23:07”

python2 中的 python3 datetime.timestamp?

我有一段python3代码,它在22:00调用一个函数。#Importsfromdatetimeimportdatetime,date,time,timedeltaimportschedimporttimeasmod_time#Findthenextdatetimecorrespondingto22:00first_run=datetime.combine(date.today(),time(22,0))first_run=first_runiffirst_run>datetime.now()elsefirst_run+timedelta(1)#Dumbtestfunctiondefm

python - 为什么 `datetime.strptime` 得到的 2015 年第 0 周星期二的日期不正确?

我在pythondatetime.strptime函数中发现了一个错误。我已经根据周数(%W)、年(%Y)和星期几(%w)。2015年第一周星期二日期错误:>>>fromdatetimeimportdatetime>>>datetime.strptime('%s%s%s'%(0,2015,1),'%W%Y%w').date()datetime.date(2014,12,29)#OK>>>datetime.strptime('%s%s%s'%(0,2015,2),'%W%Y%w').date()datetime.date(2015,1,1)#WRONG!!!>>>datetime.str

python - `datetime.now(pytz_timezone)` 什么时候失败?

deloreandocs以这种方式显示以获取给定时区的当前时间usingdatetime:fromdatetimeimportdatetimefrompytzimporttimezoneEST="US/Eastern"UTC="UTC"d=datetime.utcnow()utc=timezone(UTC)est=timezone(EST)d=utc.localize(d)d=est.normalize(EST)并将其与基于delorian的代码进行比较:fromdeloreanimportDeloreanEST="US/Eastern"d=Delorean(timezone=EST)