November 18, 2020

Python Date & Time Operations

datetime time dateutil calendar Packages in Python

import datetime as dt
from datetime import date
today = datetime.date.today()
print (dt.datetime.fromtimestamp(example))
>>> my_date = datetime.date(1982, 4, 24)
>>> my_date = datetime.date(day=24, year=2015, month=6)
str(datetime.datetime(2017, 07, 22, 19, 53, 42))
>>> my_other_datetime = datetime.datetime(year=1984, month=6, day=24, hour=18, minute=30)

print today.strftime('We are the %d, %b %Y') ==> string format time
print datetime.now().strftime("%Y-%m-%d %H:%M")
print "We are the {:%d, %b %Y}".format(today)

   %a  Locale’s abbreviated weekday name.
    %A  Locale’s full weekday name.    
    %b  Locale’s abbreviated month name.  
    %B  Locale’s full month name.
    %c  Locale’s appropriate date and time representation.
    %d  Day of the month as a decimal number [01,31].  
    %f  Microsecond as a decimal number [0,999999], zero-padded on the left
    %H  Hour (24-hour clock) as a decimal number [00,23].  
    %I  Hour (12-hour clock) as a decimal number [01,12].  
    %j  Day of the year as a decimal number [001,366].
    %m  Month as a decimal number [01,12].
    %M  Minute as a decimal number [00,59].    
    %p  Locale’s equivalent of either AM or PM.
    %S  Second as a decimal number [00,61].
    %U  Week number of the year (Sunday as the first day of the week)
    %w  Weekday as a decimal number [0(Sunday),6].
    %W  Week number of the year (Monday as the first day of the week)
    %x  Locale’s appropriate date representation.  
    %X  Locale’s appropriate time representation.  
    %y  Year without century as a decimal number [00,99].  
    %Y  Year with century as a decimal number.
    %z  UTC offset in the form +HHMM or -HHMM.
    %Z  Time zone name (empty string if the object is naive).  
    %%  A literal '%' character.

d.replace(day=26)
date(2002, 12, 4).weekday() ==> 2
date(2002, 12, 4).isoweekday() ==> 3
date(2016, 12, 4).isoformat() ==> '2016-12-04'
date(2017, 12, 4).ctime() ==> 'Wed Dec 4 00:00:00 2017'
t = d.timetuple()
ic = d.isocalendar()
dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") ==> string parse time
yesterday = today - one_day
print(getattr(datetime.datetime.now(),'year'))
print(getattr(datetime.datetime.now(),'second'))
>>> my_date = datetime.date(*[int(i) for i in string_date.split("-")])

import time
t = time(12, 30)
>>> my_time = datetime.time(hour=0, minute=0)
print (time.time()) -- current unix time
>>> date.fromtimestamp(time.time())
datetime.combine(d, t)
datetime.datetime.combine(datetime.date(2011, 01, 01), datetime.time(10, 23))
>>> df.apply(lambda x: combine(x['MEETING DATE'], x['MEETING TIME']), axis=1)
print 'Resolution:', datetime.time.resolution
time.sleep(1.5)
time.sleep(200)

from datetime import date,timedelta, tzinfo
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
>>> year = timedelta(days=365)
datetime(2017, 06, 25, tzinfo=TZ()).isoformat(' ')
dt2 = datetime(2006, 6, 14, 13, 0, tzinfo=gmt1)
print "milliseconds:", datetime.timedelta(milliseconds=1)
>>> yesterday = today - datetime.timedelta(1)
>>> [d.date() for d in pd.to_datetime(dates)]
>>> pd.to_datetime(datetimes).to_pydatetime().tolist()

from dateutil import parser
dt = parser.parse("Aug 28 1999 12:00AM")
>>> dates.append('12 1 2017')

import easy_date
import date_converter
import arrow

import calendar
cal = calendar.LocaleTextCalendar(locale = "de")
txt = cal.formatyear(startyear, m = 4)
txt = cal.formatyear(startyear + 1, m = 4)

Related 
Python Articles:   Python numpy package      bokeh glyph packages in Python

1 comment:

  1. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man learn Python Online Course

    ReplyDelete