Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of StringAndDate in python

Detailed explanation of the usage of StringAndDate in python

Y2J
Y2JOriginal
2017-05-13 13:39:361269browse

This article mainly introduces relevant information on simple examples of converting StringAndDate between python strings and dates. Friends in need can refer to

Converting between python strings and dates StringAndDate

The implementation code is given here and can be used directly. You can take a look.

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

[Related recommendations]

1. Special recommendation:"php programmer Toolbox" V0.1 version download

2. Python free video tutorial

3. Python object-oriented video tutorial

The above is the detailed content of Detailed explanation of the usage of StringAndDate in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn