封装脚本:

#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. ARM承认芯片漏洞:披露修复细节

    在谷歌安全研究人员曝光了影响整个芯片产业的CPU设计漏洞后,ARM的Cortex系列处理器也未能逃过一劫.在一篇致开发者的博客文章中,该公司披露了三个已知漏洞的细节——其中两个与Spectre有关.第 ...

  2. PyQt4将窗口放在屏幕中间

    以下脚本显示了将窗口放在屏幕中间位置的方法. #!/usr/bin/python # -*- coding:utf-8 -*- import sys from PyQt4 import QtGui c ...

  3. JS-【同页面多次调用】tab选项卡封装

    这两天遇到一个页面,同一个页面中同一个特效会用好多次,比如tab,比如轮播等.我又不想很不负责任的复制一遍代码,那样页面臃肿,自己心里也堵得慌.于是就想着把代码封装起来多次调用. 对于封装,只在公开课 ...

  4. CVE-2017-1000117命令注入验证

    首先,我们来看第一个问题,就是ssh的一种操作. ssh -oProxyCommand=gnome-calculator xxx 问题的本质在于ssh会把//后的host那么部分带-号的按照参数指令去 ...

  5. ansible的携带密码访问

    author:head森  chen date: 2018-08-13  10:28:34 1,ansible的安装 yum -y install epel-release yum -y instal ...

  6. AStar A* A星 算法TypeScript版本

    一 演示效果 二  参考教程 <ActionScript3.0 高级动画教程> + 源码 http://download.csdn.net/download/zhengchengpeng/ ...

  7. iOS5 ARC学习笔记:strong、weak等详解

    2013-03-25 13:41 佚名 oschina 字号:T | T iOS5中加入了新知识,就是ARC,其实我并不是很喜欢它,因为习惯了自己管理内存.但是学习还是很有必要的.现在我们看看iOS5 ...

  8. 从零搭建 vue-cli 脚手架

    前言: 用了几次 vue-cli 做 vue 项目,感觉没什么大问题,虽然也没有用 vue-router 和 vuex .但是心里一直有个梗,就是最初的目录生成和配置文件,一直没动过,也不知道具体原理 ...

  9. 08.Curator缓存

        可以利用ZooKeeper在集群的各个节点之间缓存数据.每个节点都可以得到最新的缓存的数据.Curator提供了三种类型的缓存方式:Path Cache,Node Cache 和Tree Ca ...

  10. jquery操作select标签change事件

    $('#update_supply_id').on('change',function(){//判断是否选取prompt属性,无返回值: if($(this).val()){ var selectTe ...