python获取当前日期和时间的方法

yipeiwu_com6年前Python基础

本文实例讲述了python获取当前日期和时间的方法。分享给大家供大家参考。具体如下:

import datetime
# Get a datetime object
now = datetime.datetime.now()
# General functions 
print "Year: %d" % now.year
print "Month: %d" % now.month
print "Day: %d" % now.day
print "Weekday: %d" % now.weekday()
# Day of week Monday = 0, Sunday = 6
print "Hour: %d" % now.hour
print "Minute: %d" % now.minute
print "Second: %d" % now.second
print "Microsecond: %d" % now.microsecond
# ISO Functions
print "ISO Weekday: %d" % now.isoweekday()
# Day of week Monday = 1, Sunday = 7
print "ISO Format: %s" % now.isoformat()
# ISO format, e.g. 2010-12-24T07:10:52.458593
print "ISO Calendar: %s" % str(now.isocalendar())
# Tuple of (ISO year, ISO week number, ISO weekday)
# Formatted date
print now.strftime("%Y/%m/%d")

希望本文所述对大家的Python程序设计有所帮助。

相关文章

浅谈Python的条件判断语句if/else语句

计算机之所以能做很多自动化的任务,因为它可以自己做条件判断。 比如,输入用户的年龄,根据年龄打印不同的内容。。。 Python程序中,能让计算机自己作出判断的语句就是if语句: 例:...

Python动态声明变量赋值代码实例

这篇文章主要介绍了Python动态声明变量赋值代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 通过exec()、globals...

使用tensorflow实现线性svm

本文实例为大家分享了tensorflow实现线性svm的具体代码,供大家参考,具体内容如下 简单方法: import tensorflow as tf import numpy a...

python模块导入的细节详解

python模块导入的细节详解

python模块导入细节 本文主要介绍了关于python模块导入的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 官方手册:https://docs.pytho...

初次部署django+gunicorn+nginx的方法步骤

环境 ubuntu16.04 python3.6 django1.11 gunicorn19.7.1 nginx1.10.3 写在前面 其实无论是在部署,还是在其他方面,我们总会遇到一...