将python时间戳转换为一年中的某天 [英] Converting python time stamp to day of year

查看:122
本文介绍了将python时间戳转换为一年中的某天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将python时间戳转换为一年中的某天:

How can I convert a python timestamp into day of year:

Timestamp('2015-06-01 00:00:00')

我想要一个数字,其中1月1日为1,1月2日为2 ... 12月31日为365(非-年)

I want a number where Jan 1 is 1, Jan 2 is 2... Dec 31 is 365 (for a non-leap year)

推荐答案

您可能想看看函数 datetime.timetuple() ,它返回 time.struct_time 具有所需属性的对象。它具有一个命名的元组接口,因此您可以按索引或属性名称访问值。

You might want to take a look at the function datetime.timetuple() which returns a time.struct_time object with your desired attribute. It has a named tuple interface, so you can access the values by index or attribute name.

import datetime

date = datetime.datetime.strptime("2015-06-01 00:00:00",
                                  "%Y-%m-%d %H:%M:%S")
print date.timetuple().tm_yday
#=> 152

这篇关于将python时间戳转换为一年中的某天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆