Python 2.7.3 Time与DateTime格式化

 1 import time
 2 import datetime
 3 
 4 class TimeX:
 5     '''时间工具,目前用于格式化时间'''
 6 
 7     @staticmethod
 8     def GetLocalTimeString_Time(arg_time=time.time()):
 9         '''返回【年-月-日 时:分:秒】格式。如果没有参数,则默认为当前时间'''
10         return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(arg_time));
11 
12     @staticmethod
13     def GetLocalTimeString_DateTime(arg_datetime=datetime.datetime.now()):
14         '''返回【年-月-日 时:分:秒】格式。如果没有参数,则默认为当前时间'''
15         return arg_datetime.strftime('%Y-%m-%d %H:%M:%S' )

 


用法:

格式化time:

timeString = TimeX.GetLocalTimeString_Time()

此时timeString为这种类型【1999年03月22日 15时22分08秒】

 

格式化datetime:

datetime_string = TimeX.GetLocalTimeString_DateTime()

此时timeString为这种类型【1999年03月22日 15时22分08秒】

posted on 2014-01-13 16:45  xxxteam  阅读(526)  评论(0编辑  收藏  举报

导航