python 日期格式只取年月_转换日期格式python - 异常日期格式 - 提取%Y%M%D

I have a large data set with a variety of Date information in the following formats:

DAYS since Jan 1, 1900 - ex: 41213 - I believe these are from Excel http://www.kirix.com/stratablog/jd-edwards-date-conversions-cyyddd

YYDayofyear - ex 2012265

I am familiar with python's time module, strptime() method, and strftime () method. However, I am not sure what these date formats above are called on if there is a python module I can use to convert these unusual date formats.

Any idea how to get the %Y%M%D format from these unusual date formats without writing my own calculator?

Thanks.

解决方案

You can try something like the following:

In [1]: import datetime

In [2]: s = '2012265'

In [3]: datetime.datetime.strptime(s, '%Y%j')

Out[3]: datetime.datetime(2012, 9, 21, 0, 0)

In [4]: d = '41213'

In [5]: datetime.date(1900, 1, 1) + datetime.timedelta(int(d))

Out[5]: datetime.date(2012, 11, 2)

The first one is the trickier one, but it uses the %j parameter to interpret the day of the year you provide (after a four-digit year, represented by %Y). The second one is simply the number of days since January 1, 1900.

This is the general conversion - not sure of your input format but hopefully this can be tweaked to suit it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值