• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Python Tutorial
  • Python Basics
  • Python Code Examples


You are here: Home / Basics / Date and Time in Python

Date and Time in Python

Last Updated: August 27, 2020

Date and Time in Python

In my last post about datetime & time modules in Python, I mostly used the strftime(format) method to print out the dates and times.

In this post, I will show you how you can do it without that by just using datetime.datetime.now().

The second part of the post is about the the timedelta class, in which we can see the difference between two date, time, or datetime instances to microsecond resolution.

Date and Time Script

The last example is a short script for counting how many days there is left for any given date (birthday in this example).


import datetime
now = datetime.datetime.now()
print "-" * 25
print now
print now.year
print now.month
print now.day
print now.hour
print now.minute
print now.second

print "-" * 25
print "1 week ago was it: ", now - datetime.timedelta(weeks=1)
print "100 days ago was: ", now - datetime.timedelta(days=100)
print "1 week from now is it: ",  now + datetime.timedelta(weeks=1)
print "In 1000 days from now is it: ", now + datetime.timedelta(days=1000)

print "-" * 25
birthday = datetime.datetime(2012,11,04)

print "Birthday in ... ", birthday - now
print "-" * 25

You should see an output simimlar to this:

-------------------------
2012-10-03 16:04:56.703758
2012
10
3
16
4
56
-------------------------
The date and time one week ago from now was:  2012-09-26 16:04:56.703758
100 days ago was:  2012-06-25 16:04:56.703758
One week from now is it:  2012-10-10 16:04:56.703758
In 1000 days from now is it:  2015-06-30 16:04:56.703758
-------------------------
Birthday in ...  31 days, 7:55:03.296242
-------------------------

I found this blog post at saltycrane.com that I liked and want to share: http://www.saltycrane.com/blog/2008/06/how-to-get-current-date-and-time-in/

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial

Filed Under: Basics, Date and Time Date Originally Published: October 4, 2012

More Python Topics

API Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line crawler Data Types Development Dictionary Dictionary Data Structure In Python envoy Errorhandling Error Handling Exceptions Fabric Files fnmatch ftplib Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pil pip Python Python Code Snippets Python On The Web Python Strings Requests Scraping Scripts sh simplehttpserver System & OS urllib2 Web

Primary Sidebar

Get Our Free Guide To Learning Python

Menu

  • Python Basics
  • Code Examples
  • Loops
  • Functions
  • Strings
  • Python on the Web
  • Lists
  • Dictionaries
  • Python Modules
  • Python Glossary
  • Learn Python

Most Popular Content

  • Reading and Writing Files in Python
  • String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use Split in Python
  • How to use comments in Python
  • Python Syntax Basics

Recent Posts

  • Datacamp Review 2020
  • Most Common Python Interview Questions For 2020
  • Python 2 Vs Python 3 with Examples
  • How To Run Your Python Scripts
  • The 5 Best Python IDE’s and Code Editors for 2019

Python Courses

  • Datacamp: Intro To Python
  • 2021 Complete Python Bootcamp
  • Python Mega Course: Build 10 Real World Apps
  • Python Data Science Bootcamp
  • Complete Python Developer: Zero to Mastery

Copyright © 2012–2021 ยท PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us