封装脚本:

#encoding=utf-8
import time
from datetime import timedelta,date

def date_time_chinese():
    print u"returns the current time string,format for YYYY年mm月dd日HH时MM分SS秒"
    return time.strftime("%Y年%m月%d日 %H时%M分%S秒".decode('utf-8').encode('gbk'),time.localtime())

def date_chinese():
    print u"returns the current time string, format for YYYY年mm月dd日"
    return time.strftime("%Y年%m月%d日".decode('utf-8').encode('gbk'),time.localtime())

def time_chinese():
    print u"returns the current time string,format for HH时 MM分 SS秒"
    return time.strftime("%H时%M分%S秒".decode('utf-8').encode('gbk'),time.localtime())

def date_time():
    print "returns the current time string, format for YYYY-mm-dd HH:MM:SS"
    return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

def date_time_slash():
    print "returns the current time string,format for YYYY/mm/dd HH:MM:SS"
    return time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())

def dates():
    print "returns the current time string,format for YYYY-mm-dd"
    return time.strftime("%Y-%m-%d",time.localtime())

def date_slash():
    print "returns the current time string,format for YYYY/mm/dd"
    return time.strftime("%Y/%m/%d",time.localtime())

def times():
    print "returns the current time string, format for HH:MM:SS"
    return time.strftime("%H:%M:%S",time.localtime())

def year():
    print "returns the current time string,format for year"
    return time.strftime("%Y",time.localtime())

def month():
    print "returns the current time string,format for month"
    return time.strftime("%m",time.localtime())

def day():
    print "returns the current time string,format for day"
    return time.strftime("%d",time.localtime())

def hour():
    print "returns the current time string, format for Hour"
    return time.strftime("%H",time.localtime())

def minute():
    print "returns the current time string,format for minute"
    return time.strftime("%M",time.localtime())

def seconds():
    print "return the current time string,format for seconds"
    return time.strftime("%S",time.localtime())

def str_to_tuple(stime):
    print "returns the string variable into time tuples"
    return time.strptime(stime,"%Y-%m-%d %H:%M:%S")

def add_date(day_num):
    today=date.today()
    print "returns the current date-%s and a time interval-%s" %(today,day_num)
    times=today+timedelta(days=day_num)
    return times

def sub_date(day_num):
    today=date.today()
    print "returns the current date-%s minus one time interval-%s" %(today,day_num)
    times=today-timedelta(days=day_num)
    return times

if __name__=='__main__':
    print date_time_chinese()
    print date_chinese()
    print time_chinese()
    print date_time()
    print date_time_slash()
    print dates()
    print date_slash()
    print times()
    print year()
    print month()
    print day()
    print hour()
    print minute()
    print seconds()
    time1=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
    print str_to_tuple(time1)    
    print add_date(2)
    print sub_date(2)

结果:

D:\test>python test2.py
returns the current time string,format for YYYY年mm月dd日HH时MM分SS秒
2018年07月04日 22时19分32秒
returns the current time string, format for YYYY年mm月dd日
2018年07月04日
returns the current time string,format for HH时 MM分 SS秒
22时19分32秒
returns the current time string, format for YYYY-mm-dd HH:MM:SS
2018-07-04 22:19:32
returns the current time string,format for YYYY/mm/dd HH:MM:SS
2018/07/04 22:19:32
returns the current time string,format for YYYY-mm-dd
2018-07-04
returns the current time string,format for YYYY/mm/dd
2018/07/04
returns the current time string, format for HH:MM:SS
22:19:32
returns the current time string,format for year
2018
returns the current time string,format for month
07
returns the current time string,format for day
04
returns the current time string, format for Hour
22
returns the current time string,format for minute
19
return the current time string,format for seconds
32
returns the string variable into time tuples
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=4, tm_hour=22, tm_min=19, tm_sec=32, tm_wday=2, tm_yday=185, tm_isdst=-1)
returns the current date-2018-07-04 and a time interval-2
2018-07-06
returns the current date-2018-07-04 minus one time interval-2
2018-07-02

注意点:

涉及到打印中文时间字符串时,要注意一下字符编码,下边这段代码中的.decode('utf-8').encode('gbk')如果不加就会报下边的错,

原因是因为中文字符在文件中存的是utf-8编码格式,在运行时python处理器会试图用utf-8编码显示中文字符,但是中文字符需要用gbk编码才会正常显示,所以就会报错,这时可以用utf-8编码把中文字符转解码(decode)unicode编码格式,在编码(encode)成gbk格式就可以了,好多中文字符的问题都是这个方法来解决的,后续本人会整理一下解决中文字符显示的三部曲来供大家参考

def date_time_chinese():
    print u"returns the current time string,format for YYYY年mm月dd日HH时MM分SS秒"
    return time.strftime("%Y年%m月%d日 %H时%M分%S秒".decode('utf-8').encode('gbk'),time.localtime())

不加.decode('utf-8').encode('gbk')时报错如下:
D:\test>python test2.py
2018骞?7鏈?4鏃?22鏃?1鍒?1绉扵raceback (most recent call last):
  File "test2.py", line 83, in <module>
    date_time_chinese()
  File "test2.py", line 11, in date_time_chinese
    print time.strftime("%Y骞?m鏈?d鏃?%H鏃?M鍒?S绉?,time.localtime())
IOError: [Errno 0] Error

python 封装时间常用操作方法-time,datetime的更多相关文章

  1. python中时间处理标准库DateTime加强版库:pendulum

    DateTime 的时区问题 Python的datetime可以处理2种类型的时间,分别为offset-naive和offset-aware.前者是指没有包含时区信息的时间,后者是指包含时区信息的时间 ...

  2. 基于python的selenium常用操作方法(1)

    1 selenium定位方法    Selenium提供了8种定位方式. ·         id ·         name ·         class name ·         tag ...

  3. python列表的常用操作方法

    主要介绍了Python中列表(List)的详解操作方法,包含创建.访问.更新.删除.其它操作等,需要的朋友可以参考下. 1.创建列表.只要把逗号分隔的不同的数据项使用方括号括起来即可 List = [ ...

  4. python字典的常用操作方法

    Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串.数字.元组等其他容器模型.本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建.访问.删除.其它操作等,需 ...

  5. 基于python的selenium常用操作方法(2)

    9 多表单切换 在Web应用中经常会遇到frame/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于frame/iframe表单内嵌页面上的元素无法直接定位.这 ...

  6. Python常用模块-时间模块(time&datetime)

    Python常用模块-时间模块(time & datetime) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.初始time模块 #!/usr/bin/env pyth ...

  7. 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码

    本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...

  8. python时间模块-time和datetime

    时间模块 python 中时间表示方法有:时间戳,即从1975年1月1日00:00:00到现在的秒数:格式化后的时间字符串:时间struct_time 元组. struct_time元组中元素主要包括 ...

  9. Python常用模块(time, datetime, random, os, sys, hashlib)

    time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) :         通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运 ...

随机推荐

  1. Kindeditor放置两个调用readonly错误

    开始 需要调用Kindeditor中的readonly的方法,但是一直提示edit is undefined 而editor.readonly(true)又只对第一个对象有效 所以只能换换形式,干脆将 ...

  2. poj_3283 trie树

    题目大意 将一副牌进行编号,四种花色分别标记为'C'.'D'.'H'.'S',数值标记为'A'.'1'.'2'.'3'.'4'.'5'.'6'.'7'.'8'.'9'.'10'.'J'.'Q'.'K' ...

  3. hihocoder [Offer收割]编程练习赛14 可疑的记录

    题目3 : 可疑的记录 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi有一棵N个节点的树,编号1-N,其中1号节点是整棵树的根.他把这棵树的N-1条边记录成N-1 ...

  4. ExtJS学习

    ExtJS是一门比较纠结的框架,自己不太熟,因为现在在做一些老项目,所以没办法要学点.记录下.其实Ext也不是很难,主要是多查查API,了解其基本的用法,然后慢慢去学习,学成之后做管理系统还是很有优势 ...

  5. ThinkPHP分类查询(获取当前分类的子分类,获取父分类,下一级分类)

    获取指定分类的所有子分类ID号 //获取指定分类的所有子分类ID号 function getAllChildcateIds($categoryID){ //初始化ID数组 $array[] = $ca ...

  6. Spring集成Struts、Hibernate----三大框架SSH(Spring、Struts和hibernate)

    Spring 框架可以完成业务层的设计任务,Struts框架可以将表示层和业务层分离,而Hibernate框架可以提供灵活的持久层支持.下面介绍三大框架的集成环境: 1.配置Struts2. I.导入 ...

  7. 前端模拟(mock)接口数据(koa)

    在前后端分离开发项目时,经常会有前后端进度不一致,可能前端界面开发已经完成,就等接口了,如果等接口出来再联调的话时间可能会来不及. 这个时候,前端就可以根据制定好的接口规范和接口文档来mock接口数据 ...

  8. 理解 php new static

    今天在看 Laravel 的容器(Container)实现时,发现了这么一段突然不能理解的代码: ** * Set the globally available instance of the con ...

  9. pta 习题集5-18 打印学生选课清单

    假设全校有最多40000名学生和最多2500门课程.现给出每门课的选课学生名单,要求输出每个前来查询的学生的选课清单. 输入格式: 输入的第一行是两个正整数:N(≤≤40000),为前来查询课表的学生 ...

  10. SQL---->mySQl安装for mac

    我安装是参考如下两篇博客,但是有些不同,这里写好参考来源: http://blog.csdn.net/li_huifeng/article/details/9449685 http://www.jia ...