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

Detailed explanation of the usage of StringAndDate in python

Y2J
Release: 2017-05-13 13:39:36
Original
1318 people have browsed it

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
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template