python string and date conversion between StringAndDate

  • 2020-05-30 20:34:33
  • OfStack

Convert StringAndDate between the python string and the date

Here is the implementation code, can be used directly. You can look at it.

Example code:

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 = 
    dafter = dnow + datetime.timedelta(hours=n) 
    return dafter 

Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: