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 对key为时间的dict排序方法

如下所示: import time def date_compare(item1, item2): t1 = time.mktime(time.strptime(item1,...

python实战串口助手_解决8串口多个发送的问题

今晚终于解决了串口发送的问题,更改代码如下: def write(self, data): if self.alive: if self.serSer.isOpe...

python 计算数组中每个数字出现多少次--“Bucket”桶的思想

python 计算数组中每个数字出现多少次--“Bucket”桶的思想

题目: 解法一:比较元素是否相等 思路说明: 这种应该是普通人最先想到的解法,先获取到数组之后进行有小到大排序,然后初始化一个min=0(代表新数字的开始角标),然后遍历新数组的每一个...

python 编程之twisted详解及简单实例

python 编程之twisted详解 前言:  我不擅长写socket代码。一是用c写起来比较麻烦,二是自己平时也没有这方面的需求。等到自己真正想了解的时候,才发现自己在这方...

查看django版本的方法分享

在cmd输入: python -m django --version 以上这篇查看django版本的方法分享就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持...