Table of contents
Introduction
Welcome to my 15th blog post. Today I learnt some very cool stuff on the TIME module of my python coding journey. I also built a small python project using TIME modules and learnings from the past few days. Now let's dive deep into the details
So let's get started......
Time Module
The Python Time module provides us with various ways to represent time in code. Using the time module we can fetch the current time, day and date and also do some cool things using the time module.
Project 2 -
In today's project, I wrote a program to greet Good morning, Good afternoon, Good evening, and Good night based on the current time.
In this project, I made use of the time module and also used the time module's in-built method time.strftime()
time.strftime()
You may find yourself in a position where the time format of the time isn’t satisfactory for your application. Instead, you may want to format your strings to make them more readable
So to format strings, we make use of strftime(). It stands for “string format time.”
It takes two arguments:
format
specifies the order and form of the time elements in your string.t
is an optional time tuple.
To format a string, you use directives. Directives are character sequences that begin with a %
that specify a particular time element, such as:
%d: Day of the month
%m: Month of the year
%Y: Year
%H: Hour of the day
%M: Minutes of the day
%S: Seconds of the day
Note: The strftime() function returns the output in the 'string' format, hence we need to TYPECAST the output into 'int' datatype to perform any sort of mathematical or conditional operations.
CODE
import time
name = input("Enter your Name \n ")
timestamp = time.strftime("%H:%M:%S")
print("\n", timestamp)
hours = int(time.strftime('%H'))
print(hours)
minutes = int(time.strftime('%M'))
print(minutes)
seconds = int(time.strftime('%S'))
print(seconds)
if (hours>4 and hours < 12 ):
print("goodmorning")
elif(hours>12 and hours<16):
print("good afternoon")
elif(hours>16 and hours<21):
print("good evening")
else:
print("goodnight")
print("Hi ", name, "time now is ", timestamp, )
Output
Enter your Name
Chintan
22:37:23
22
37
23
goodnight
Hi Chintan time now is 22:37:23
PS D:\coding\python\instagram\Python - Day #15>
Resources Used
You can watch the video of Day#15 by clicking on the below link 👇👇👇👇👇
Conclusion
Thanks, guys for going through this blog post. Learnt something cool things about the time module and how to use it. Also built a simple project 2 and this makes me feel empowered to learn to code and build more
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding This is it for Day #15
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practising coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.