简体   繁体   English

python中箭头库的不同日期结果

[英]Different date results from arrow library in python

(Pdb) arrow.get('2016-01-01')
<Arrow [2016-01-01T00:00:00+00:00]>
(Pdb) arrow.get('20160101')
<Arrow [1970-08-22T08:01:41+00:00]>

So, I want my function to be able to use arrow library in python to parse date strings. 因此,我希望我的函数能够在python中使用arrow库来解析日期字符串。 However, as can be seen from above code, it gives different results based on whether - is present in the date string or not. 但是,从上面的代码可以看出,它基于日期字符串中是否存在-给出了不同的结果。 How can I modify it so that it gives same results for both? 如何修改它,以使两者都得到相同的结果?

Simply provide a format string for the second object 只需为第二个对象提供格式字符串

arrow.get('20160101', 'YYYYMMDD')

The library probably defaults to the iso standard date format, but if you arent using that, you need to tell it how to interpret the string. 该库可能默认为iso标准日期格式,但是如果您不使用它,则需要告诉它如何解释字符串。

In the REPL 在REPL中

>>> arrow.get('2016-01-01')
<Arrow [2016-01-01T00:00:00+00:00]>
>>> arrow.get('20160101', 'YYYYMMDD')
<Arrow [2016-01-01T00:00:00+00:00]>

To use common call when the dashes are present or not, you could strip out the dashes 要在出现或不出现破折号的情况下使用常规呼叫,可以将破折​​号去除

import arrow
vals = ['2016-01-01', '20160101']

for v in vals:
    d = v.replace('-', '')
    print(arrow.get(d, 'YYYYMMDD'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM