python datetime module | python datetime tutorials | Second Part

Namaste Friends,




                             Friends, This Is Our Second Part of Python Date And Time Tutorials Series. And In This Post, I Going To Show You How To Use Python DateTime Module With Practical Examples. 


But First If You Are New Visitor Then Click Here For First Part

or 

Check Our Blog Post Index. 



So, let's start with some practical usages examples.

datetime_part_2_(dates_time) slides
In [1]:
#!/usr/bin/python
#
# ============================================
#       PYTHON DATETIME MODULE TUTORIAL
# ============================================
#
# Author : SSB
#          surajsinghbisht054@gmail.com
#          https://bitforestinfo.blogspot.com
# 
#
# Here, I am Using
# 
# Operating System : Ubuntu 16.04 LTS
# Python Version   : 2.7.12
# Editor           : ipython notebook
#
#
In [2]:
#
# Import Module
#
import datetime
In [3]:
#
# Today's Date and Time Both
#
now = datetime.datetime.now()
In [4]:
#
# print in representable form
#
print repr(now)
datetime.datetime(2017, 3, 30, 16, 36, 31, 610206)
In [5]:
#
# Print Is String Form
#
print str(now)
2017-03-30 16:36:31.610206
In [6]:
#
# Here, I am Showing How We Can Seperate
# Different Values.
#
# Week day
#
WEEKEND = {
    0 : "Monday",
    1 : "Tuesday",
    2 : "Wednesday",
    3 : "Thursday",
    4 : "Friday",
    5 : "Saturday",
    6 : "Sunday"
}

print " Date   :",now.day
print " Month  :",now.month
print " Year   :",now.year
print " Day    :",WEEKEND[now.weekday()]
print " Hour   :",now.hour
print " Minute :",now.minute
print " Second :",now.second
 Date   : 30
 Month  : 3
 Year   : 2017
 Day    : Thursday
 Hour   : 16
 Minute : 36
 Second : 31
In [7]:
#
# Printing Date And Time In Specified Format.
#   if You not Understand. what going on.
#    then, firt read previous tutorial.
#
print now.strftime("%a, %d %b %Y %I:%M:%S %p %Z")
Thu, 30 Mar 2017 04:36:31 PM 
In [8]:
#
# Get Today's Date Only
#
date = now.date()
#
# Print Date Object 
#
print repr(date)," <=> " ,date
#
# Get Current Time Only
#
time = now.time()
#
# Print Time object Only
#
print repr(time)," <=> " , time
datetime.date(2017, 3, 30)  <=>  2017-03-30
datetime.time(16, 36, 31, 610206)  <=>  16:36:31.610206
In [9]:
#
# Now, Combine Both Date and time together.
#
combo = datetime.datetime.combine(date, time)
#
# Print Date and time object
#
print combo
#
# print in specified format
#
print now.strftime("%a, %d %b %Y %I:%M:%S %p %Z")
2017-03-30 16:36:31.610206
Thu, 30 Mar 2017 04:36:31 PM 
In [10]:
#
# Get Today's Date Only
#
today = datetime.date.today()
In [11]:
#
# Here, I am Showing how to get values seperately
#
print " Date   :",today.day
print " Month  :",today.month
print " Year   :",today.year
print " Day    :",today.weekday()
 Date   : 30
 Month  : 3
 Year   : 2017
 Day    : 3
In [12]:
#
# Print today
#
print today
2017-03-30
In [13]:
#
# Here, I am replacing some values of today variable,
#
t = today.replace(year=2018, day=25, month=12)
#
# print replaced values
#
print t
2018-12-25
In [14]:
#
# print today's value as ctime()
#
print today.ctime()
Thu Mar 30 00:00:00 2017
In [15]:
#
# get today's time values as struct time.
#
t = today.timetuple()
In [16]:
#
# print struct time
#
print t
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=89, tm_isdst=-1)
In [17]:
#
# create a new date object
#
dt = datetime.date(1995, 11, 25)
#
# print date object
#
print dt
#
# create new time object
#
tt = datetime.time(12, 56, 40)
#
# print time object
#
print tt
#
# Combine both new objects together
#
t = datetime.datetime.combine(dt, tt)
#
# print combined objects
#
print t
1995-11-25
12:56:40
1995-11-25 12:56:40
In [18]:
#
# print combined objects values
#
# Convert date and time in struct format
#
t = t.timetuple()
#
print " Year          =", t.tm_year
print " Day of Year   =", t.tm_yday
print " Weekday       =", t.tm_wday
print " Second        =", t.tm_sec
print " Hour          =", t.tm_hour
print " Date of Month =", t.tm_mday
print " Minute        =", t.tm_min
print " Month         =", t.tm_mon
 Year          = 1995
 Day of Year   = 329
 Weekday       = 5
 Second        = 40
 Hour          = 12
 Date of Month = 25
 Minute        = 56
 Month         = 11
In [19]:
#
# You Can Also Create New Object With These type of arguments
date = datetime.date(year=2018, day=25, month=12)
#
time = datetime.time(hour=12, second=40)
In [20]:
print repr(time)
print repr(date)
print time
print date
datetime.time(12, 0, 40)
datetime.date(2018, 12, 25)
12:00:40
2018-12-25








Python DateTime tutorials Part Two Ends Here.
For Next Part Click Here.



Thanks For Reading,

For More Updates And Tutorials,
Subscribe our blog,
like, comment and share.


Have a Wonderful day
SSB :) 

Share this

Related Posts

Previous
Next Post »