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

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

问题描述

我想在 Python 中获取当前时间并将它们分配给诸如 yearmonthdayhour 之类的变量分钟.这如何在 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天全站免登陆