【python】time,datetime,string相互转换

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

 

 

 

posted on 2017-11-13 18:30  星河赵  阅读(514)  评论(0编辑  收藏  举报

导航