python 字符串和日期之间转换 StringAndDate
这里给出实现代码,直接可以使用。大家可以看下。
实例代码:
'''''
Created on 2013-7-25
@author: Administrator
'''
from datetime import datetime
class StringAndDate(object):
''''' String to Date(datetime) or date to string '''
def stringToDate(self,string):
#example '2013-07-22 09:44:15+00:00'
dt = datetime.strptime(string, "%Y-%m-%d %H:%M:%S+00:00")
#print dt
return dt
''''' Date(datetime) to String '''
def dateToString(self,date):
ds = date.strftime('%Y-%m-%d %H:%M:%S')
return ds
''''' return n hours after datetime '''
def getAfterDate(self,n):
dnow = datetime.datetime.now()
dafter = dnow + datetime.timedelta(hours=n)
#dafter.ctime()
return dafter
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8