Python ->
Test use of date-time functions in Python

Simple example: Test use of date-time functions in Python 2.7.

# ------------------------------------------------
# test datetime methods
# ------------------------------------------------

import datetime

def calc_days_diff(a,b):

   A = a.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
   B = b.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
   return (A - B).days


def main():
   # main begin

   # get current date-time
   now_dt = datetime.datetime.now()

   print
   print (now_dt)
   # date-time parts:
   print 'date-time parts:'
   print (now_dt.year, now_dt.month, now_dt.day,
    now_dt.hour, now_dt.minute, now_dt.second, now_dt.microsecond)

   print ('{}/{}/{} {}:{}:{}.{}'.format(
    now_dt.year, now_dt.month, now_dt.day,
    now_dt.hour, now_dt.minute, now_dt.second, now_dt.microsecond))

   # calculate time difference from 01-01-1970
   d1 = datetime.datetime(1970, 01, 01, 00, 00)
   d2 = datetime.datetime.now()

   print
   print 'time difference in days:'
   print (d2 - d1).days # 1
   print calc_days_diff(d2, d1) # 2

   print 'time difference in years:'
   print ((d2 - d1).days / 365)

   # main end


if __name__ == "__main__": main()




sqlexamples.info