Python - Date

> Procedural Languages > Python > Python - Data Type

2 - Example

from datetime import datetime
 
now = datetime.now()
print now.month
print now.day
print now.year
print str(now.month)+'/'+ str(now.day) + '/'+str(now.year)
print str(now.hour)+':'+ str(now.minute) + ':'+str(now.second)

3 - Conversion

3.1 - String

def parse_apache_time(s):
    """ 
    Args:
        s (str): date and time in Apache time format
    Returns:
        datetime: datetime object (ignore timezone for now)
    """
    return datetime.datetime(int(s[7:11]),
                             month_map[s[3:6]],
                             int(s[0:2]),
                             int(s[12:14]),
                             int(s[15:17]),
                             int(s[18:20]))
Advertising