Python Tutorial: Datetime Module – How to work with Dates, Times, Timedeltas, and Timezones

///Python Tutorial: Datetime Module – How to work with Dates, Times, Timedeltas, and Timezones

Python Tutorial: Datetime Module – How to work with Dates, Times, Timedeltas, and Timezones

FavoriteLoadingAdd to favorites

In this Python Tutorial, we will be learning how to use the datetime module. The datetime module is important to understand, because you will be working with dates and times in just about every application you write. We will look at how to work with dates, times, datetimes, timedeltas, and timezones. Let’s get started.

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2020-10-25T10:16:50+00:00October 25th, 2020|Python Video Tutorials|46 Comments

46 Comments

  1. Heshan Kumarasinghe October 25, 2020 at 10:17 am - Reply

    Thank you… This video is helpful…. Gonna watch it again sometime…..

  2. Aaron Barber October 25, 2020 at 10:17 am - Reply

    Awesome!!! Clear, concise, and quick. As a person new to python, this is exactly what I needed. It will probably take me a couple viewings. THANK YOU

  3. Tanveer Singh October 25, 2020 at 10:17 am - Reply

    I really wish you had mentioned the arrow package at the start of the video as opposed to the end!

  4. Kalyani Bhandari October 25, 2020 at 10:17 am - Reply

    Bro please make a video on how to take Nepali date(Bs) in python

  5. Rakesh Kodagali October 25, 2020 at 10:17 am - Reply

    Belated Wishes Happy Birthday @corey

  6. Hotshots October 25, 2020 at 10:17 am - Reply

    Happy Birthday Corey

  7. Luqman Hakim October 25, 2020 at 10:17 am - Reply

    hey Corey ! I just wanted to tell you that I immediately pause watching this video at minute 6:30 because you said your birthday is on 24 September which is the same with me. I'm just happy to have a same birthday date with an amazing person like you 🙂 keep up the good content. I'm younger btw hehe

  8. acidvac October 25, 2020 at 10:17 am - Reply

    -1444 days till bday! Oh yeah… I need to update the year lol

  9. Sleepy Studios October 25, 2020 at 10:17 am - Reply

    what about to show the amount of minutes, seconds, hours and days until a reaccuring date?

  10. Abe Burdock October 25, 2020 at 10:17 am - Reply

    Awesome video and very clear explanation: One thing that I noticed @ about the 21:18 mark: Corey showed that astimezone( ) cannot be applied to a naive datetime…he showed that this raises an error on his IDE.

    When I did the same thing, however, I DID NOT receive an error.

    My Code:
    dt_mountain_time = datetime.datetime.now() #this is naive time

    dt_east_time = dt_mountain_time.astimezone(pytz.timezone('US/Eastern'))

    dt_east_time2 = datetime.datetime.now(pytz.timezone('US/Eastern'))

    My Output:
    2020-09-05 07:54:59.087454

    2020-09-05 10:54:59.087454-04:00

    2020-09-05 10:54:59.089483-04:00

    I did not receive an error as you can see. It appears that I actually was able to apply astimezone() to a naive datetime. Any thoughts? Has the pytz module changed?

  11. Hussein Abukar October 25, 2020 at 10:17 am - Reply

    Very interesting time and date zone formatting. (straight to the point saving your time!!) Great

  12. Aleem Mustafavi October 25, 2020 at 10:17 am - Reply

    What is the code editor used?
    is it possible on pycharm for python 3.8 ?

  13. Yasin Yasin October 25, 2020 at 10:17 am - Reply

    Great tutorial. Thank you

  14. JayaVardhan 07 October 25, 2020 at 10:17 am - Reply

    Enjoyed Every Bit of video.
    Thanks Man

  15. Ola Söderlind October 25, 2020 at 10:17 am - Reply

    import datetime

    def getLastDayInMonth(year,month):

    if(month>11):

    month=1

    year+=1

    else:

    month+=1

    tempdate=datetime.date(year,month,1)

    tempdatedelta= datetime.timedelta(days=-1)

    return tempdate + tempdatedelta

    #End getLastDayInMonth

    print( getLastDayInMonth(2020,8) ) # output: 2020-08-31

    print( getLastDayInMonth(2020,12) ) # output: 2020-12-31

    print( getLastDayInMonth(2021,12) ) # output: 2021-12-31

    print( getLastDayInMonth(2020,2) ) # output: 2020-02-29

    print( getLastDayInMonth(2021,2) ) # output: 2021-02-28

    print( getLastDayInMonth(2021,4) ) # output: 2021-04-30

    # get days of a month

    print( getLastDayInMonth(2021,2).day ) # output: 28

  16. Ola Söderlind October 25, 2020 at 10:17 am - Reply

    my birtday is in 62days 😀

  17. Daniel Morales October 25, 2020 at 10:17 am - Reply

    Hey I am trying to use date time to create a calendar and stick them to labels on tkinter for particular months. The idea is to show for
    January
    Mon 1 in label 1, label 2 Tue 2nd, Wed 3rd….
    all the way to the end of the month. I have not been able to find a good way to do this. In fact most just show how to use datetime and calander at its basic, or to create date pickers which I don't need. Any help would be appreciated

  18. Chaos Control Media October 25, 2020 at 10:17 am - Reply

    I'm Like #4,500 apparently
    Great stuff man!

  19. PoPo Du October 25, 2020 at 10:17 am - Reply

    24:05 what is this documentation called? can you send the link?

  20. athisii_ekhe October 25, 2020 at 10:17 am - Reply

    As always, the best tutorial for the topic. Thanks

  21. Rocco Di Vincenzo October 25, 2020 at 10:17 am - Reply

    Pride of you Corey! You're one of the few ones who do no sell shitty online courses but only share your knowledge with us

  22. EV1L2 K1RM4D4 October 25, 2020 at 10:17 am - Reply

    damned timezones and time programming.

  23. jagadeesh mandala October 25, 2020 at 10:17 am - Reply

    pip install pytz giving syntax eeror in version 3.8.2

  24. Kushwanth Kandala October 25, 2020 at 10:17 am - Reply

    How to parse this string '2020-03-02T11:21:33.7489989-08:00' using strptime() and here %f not in the range of 000000-999999 '7489989' is not a micro seconds format?

  25. Kushwanth Kandala October 25, 2020 at 10:17 am - Reply

    How to parse this string '2020-03-02T11:21:33.7489989-08:00' using strptime() and here %f not in the range of 000000-999999 '7489989' is not a micro seconds format?

  26. PhisherM64 Vcx October 25, 2020 at 10:17 am - Reply

    Is necessary to learn about this module datetime ? bcz is so hard to memorize that ..Any Help Please

  27. legmeervogeltje October 25, 2020 at 10:17 am - Reply

    Omg this guys bday is the same day as my bday

  28. EUBEEN 123 October 25, 2020 at 10:17 am - Reply

    1:32 that's my birthday!

  29. Brendan October 25, 2020 at 10:17 am - Reply

    This is great. The datetime module is surprisingly confusing.

  30. Alex Landherr October 25, 2020 at 10:17 am - Reply

    Is it necessary to use aware objects if the system you are running this on is on UTC? Or should I always use aware objects? I’m running most of my code on a Raspberry Pi 4B 4 Gibibyte model with the system time set to be UTC.

  31. Vaibhav Kumar October 25, 2020 at 10:17 am - Reply

    I wonder why someone would have disliked this video 😳
    Anyways, love your amazing tutorials, pls keep it up 🤗👍

  32. Loid Tayag October 25, 2020 at 10:17 am - Reply

    Whats the differnece between these two? They output the same thing?Is the 2nd more efficient or something?
    dt_UTC = datetime.datetime.now(tz=pytz.UTC)

    dt_Wherever = dt_UTC.astimezone(pytz.timezone('Asia/Manila'))

    print(dt_Wherever)

    DT_Wherever = datetime.datetime.now(tz=pytz.timezone('Asia/Manila'))

    print(DT_Wherever)

  33. Noon Kang October 25, 2020 at 10:17 am - Reply

    But how we know is am or pm

  34. David Allen October 25, 2020 at 10:17 am - Reply

    Hi all. Datetime can be a real pain! I wrote up some complimentary documentation for managing Date, Datetime, and Timestamp in Python/Pandas. You might find it useful. Check it out: https://medium.com/@deallen7/managing-date-datetime-and-timestamp-in-python-pandas-cc9d285302ab

  35. Ming Lee October 25, 2020 at 10:17 am - Reply

    What is the IDE please?

  36. Harish K October 25, 2020 at 10:17 am - Reply

    how can we convert given GMT time (2020-05-28 15:34:10) to IST

  37. Indian Newyork Babies October 25, 2020 at 10:17 am - Reply

    How to convert current date time in to yyyy-mm-ddThh:mm:ss.000Z format ???? Please someone help me.

  38. Lalit Mohan October 25, 2020 at 10:17 am - Reply

    I wanted to confirm if pytz is available free in library or it is a third party tool

  39. Marcus Farcus October 25, 2020 at 10:17 am - Reply

    Fantastic reference! Thank you!

  40. Greg Collins October 25, 2020 at 10:17 am - Reply

    Great video!
    Question: Does pandas.DataFrame.tz_localize work the same or as well as pytz's functions? At first it seemed more basic than pytz, but I see examples on stackoverflow where they use this function and 'US/Eastern' in order to localize a timestamp index in a dataframe (which is what I'm trying to do!). It seems it would be a little easier to use pandas functionality without importing pytz, but wanted to get some pros/cons from anybody here. Thanks!

  41. Koray Tuğberk GÜBÜR October 25, 2020 at 10:17 am - Reply

    Thank you Corey!

  42. PRAGYA SHUKLA October 25, 2020 at 10:17 am - Reply

    Error -ModuleNotFound:No module named pytz
    I ran through many links on google but this error is still seen and I am not able to proceed..
    Any solutions ??

  43. Mohammed Arshad October 25, 2020 at 10:17 am - Reply

    Excellent videos. I am new to python. Used to do engineering/ scientific programming many many (*n! ) years ago. Used Fortran at that time.
    I am really enjoying learning Python with such clear videos and a user friendly language. May you "Live long and prosper " !!!!!

  44. Astro Lábio October 25, 2020 at 10:17 am - Reply

    I want to make a stopwatch. Let's say that i have 5 runners and only one track. They start running with 10 seconds from each other, because i only have one track. How do i know the lap time of the third athlete? They ran 8 laps of 400 meters. How do i know each lap time of each one? I hope you could help!

  45. Rajath Shetty October 25, 2020 at 10:17 am - Reply

    Hey Corey! Could you please make a video on the Arrow module as you've mentioned in this video. Looking forward to it. Thanks

  46. Fahmi Eshaq October 25, 2020 at 10:17 am - Reply

    Good vid

Leave A Comment

*