#!/usr/bin/python
# -*- coding:utf-8 -*-
import datetime,time
now = time.strftime('%Y-%m-%d %H:%M:%S')
print '当前时间:'+now
week = int(time.strftime('%W'))+1
print '当前周数:'+str(week)
#datetime >> string
now = datetime.datetime.now()
print 'datetime >> string: '+now.strftime('%Y-%m-%d %H:%M:%S')
#string >> datetime
str = '2014-11-04 13:05:51'
d = datetime.datetime.strptime(str,'%Y-%m-%d %H:%M:%S')
print 'string >> datetime: %s' %(d)
#datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
#两个日期相差多少天。
d1 = datetime.datetime.strptime('2012-03-05 17:41:20', '%Y-%m-%d %H:%M:%S')
d2 = datetime.datetime.strptime('2012-03-02 17:41:20', '%Y-%m-%d %H:%M:%S')
delta = d1 - d2
print '两个日期相隔天数:%s' %(delta.days)
#两个日期的时间差
d1 = datetime.datetime(2005, 2, 16)
d2 = datetime.datetime(2004, 12, 31)
print (d1 - d2).days
starttime = datetime.datetime.now()
endtime = datetime.datetime.now()
print (endtime - starttime).seconds
#今天的n天后的日期。
now = datetime.datetime.now()
delta = datetime.timedelta(days=3)
n_days = now + delta
print '今天的n天后的日期:'+n_days.strftime('%Y-%m-%d %H:%M:%S')
date="2012-04-05"
#查看date的类型<type 'str'>
print type(date)
#字符串转换成time类型
date=time.strptime(date,"%Y-%m-%d")
print type(date)
#time类型转换成字符串类型
date=time.strftime("%Y-%m-%d",date)
print type(date)
分享到:
相关推荐
datetime模块使用案例源码、# 得到当前日期时间(两种方法)、# 得到当前日期 # 得到当前时间 # 得到当前年份用year_变量接收 # 得到当前月份用month_变量接收 # 得到当前天用day_变量接收 # 使用-拼接年月日得到...
Python中提供了多个用于对日期和时间进行操作的内置模块:time模块、datetime模块和calendar模块。其中time模块是通过调用C库实现的,所以有些方法在某些平台上可能无法调用,但是其提供的大部分接口与C标准库time.h...
Python 对于日期时间的处理总共有三个模块:datetime 模块、time 模块、Calendar 模块
python基础——time模块和datetime模块
python--time模块和datetime模块.pdf
python--time模块和datetime模块.docx
datetime模块-笔记.xmind
python学习笔记,包含datetime模块-SQL数据库-对象关系映射-函数进阶
datetime是Python处理日期和时间的标准库。 获取当前日期和时间 我们先看如何获取当前日期和时间: ...注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import datetime导入的才是da
python--datetime模块基本操作
02-33-datetime模块.pdf
以 Python 的 datetime 模块为例 该资源仅供学习!!! 该资源仅供学习!!! 该资源仅供学习!!! 该资源仅供学习!!!
python使用datetime模块进行时间、日期、时间戳的转换
关于time模块和datetime模块的详细总结,可以对比并加以区分
这篇文章主要介绍了python的time模块和datetime模块实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1. 将当前时间转成字符串 strftime 方法,并输出 ...
主要介绍了Python中time模块和datetime模块的用法示例,主要演示了一些时间日期的打印和计算,需要的朋友可以参考下
相比于time模块,datetime模块的接口则更直观、更容易调用。 1、datetime中包含三个类date ,time,datetime 函数datetime.combine(date,time)可以得到dateime,datetime.date()、datetime.time()可以获得date和time 2...
在本教程中,我们将介绍 python datetime 模块以及如何使用它来处理日期、时间和日期时间格式的列(变量)。它包含各种实际示例,可帮助您增强使用 Python 函数处理日期和时间的信心。一般来说,日期类型列不容易...
Python 的datetime模块 其实就是date和time 模块的结合,常见的属性方法都比较常用 比如: datetime.day,datetime.month,datetime.year 分别表示一个datetime对象的日,月,年;如下 from datetime import ...