如何获取当前时间在python并分解成年,月,日,小时,分钟? [英] How to get current time in python and break up into year, month, day, hour, minute?

查看:218
本文介绍了如何获取当前时间在python并分解成年,月,日,小时,分钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python中获得当前时间,并将其分配给变量,如 year month 小时。如何在Python 2.7中完成?

I would like to get the current time in Python and assign them into variables like year, month, day, hour, minute. How can this be done in Python 2.7?

推荐答案

datetime 模块是你的朋友:

The datetime module is your friend:

import datetime
now = datetime.datetime.now()
print now.year, now.month, now.day, now.hour, now.minute, now.second
# 2015 5 6 8 53 40

您不需要单独的变量,返回的 datetime 对象的属性都需要您。

You don't need separate variables, the attributes on the returned datetime object have all you need.

这篇关于如何获取当前时间在python并分解成年,月,日,小时,分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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