python 封装时间常用操作方法-time,datetime

摘要:
封装脚本:#encoding=utf-8 importTimefromtatetimeimportTimedelta,datedefdate_time_Chinese():printu“返回当前时间字符串,格式为HH:MM:SS:MM:MM:DD:YYYY”returntime.strftime(“%Y:MM:DD:MM:MM:MM:SS:SM:MM:SM:MM:MM:MM:MM:MM:SS:MM:MM:YYYY”

封装脚本:

#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: est>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: est>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》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[C] c99int(让VC等编译器自动兼容C99的整数类型)V1.02。源码托管到github、添加CMake编译配置文件、使用doxygen规范注释sqlserver表被锁了,解锁方法,删除锁的方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python实例讲解 -- 获取本地时间日期(日期计算)

1. 显示当前日期:   1 #! /usr/bin/env python2 #coding=utf-83 4 import time5 6 print time.strftime('%Y-%m-%d %A %X %Z',time.localtime(time.time())) 或者 1 #! /usr/bin/env python2 #coding...

C/C++获取系统当前时间

C/C++获取系统当前时间   C库中与系统时间相关的函数定义在<time.h>头文件中, C++定义在<ctime>头文件中。 一、time(time_t*)函数 函数定义如下: time_t time (time_t* timer); 获取系统当前日历时间 UTC 1970-01-01 00:00:00开始的unix时间戳参数:...

python小专题——time模块

time常用函数 最近参与python的一个项目,发现经常遇到一些常用的模块,而每次使用时,我都要查一遍。终于,我决定要各个击破,对常用的python小知识进行总结。下面总结了python中对时间处理的常见函数。 在开始之前,首先要说明这几点: 在Python中,通常有这几种方式来表示时间:1)、时间戳 2)、格式化的时间字符串 3)、元组(struct...

docker-compose搭建mongodb分片集群(单机版)

mongodb分片机制原理 一、概念:   分片(sharding)是指将数据库拆分,将其分散在不同的机器上的过程。将数据分散到不同的机器上,不需要功能强大的服务器就可以存储更多的数据和处理更大的负载。基本思想就是将集合切成小块,这些块分散到若干片里,每个片只负责总数据的一部分,最后通过一个均衡器来对各个分片进行均衡(数据迁移)。通过一个名为mongos...

Windows的本地时间(LocalTime)、系统时间(SystemTime)、格林威治时间(UTCTime)、文件时间(FileTime)之间的转换

  首先,先从简单的说起,本地时间(LocalTime),也就是系统设置时区的当前时间!比如说当前系统设置的时区为“(UTC+08:00)北京,重庆,香港特别行政区,乌鲁木齐”(东八区),系统的右下角通知区域显示的时间为“2012/5/18 16:57”,那么这个时间就是当前系统的本地时间!   要说清楚什么是系统时间(SystemTime)之前先来了解一...

LocalDate,LocalTime,LocalDateTime的基本使用

​ Java8提供的新的时间日期的类型LocalDate,LocalTime,LocalDateTime。之前用的Date和Calendar都为线程不安全。用来格式化Date类型的SimpleDateFormat也是线程不安全的。LocalDate和格式化LocalDate的DateTimeFormatter是线程安全的。 LocalDate的使用 创建L...