#Learn #Python #Together | 🔥 Day 10 | #basics | #datetime #strftime #formatcodes #dateobjects by @techcoderlabz on 3Speak
▶️ Watch on 3Speak --- Learn Python Basics Together 🚀 | Day 10 Welcome back to our Python learning journey! In today's lesson, we learn how to work with Dates and Time in Python using the built-in...
Tags: #python #basics #learn #tutorial #beginners #programming #coding #vscode #hivesuite
Hello everyone, welcome back to my YouTube channel. So I hope that you have learned so far different things about the Python programming language, and if you haven't checked my earlier videos, please click the link below, which is for the playlist for the Python learning journey. Today we are going to talk about dates. So let's get started. First thing first you have to do is import the package for the date. It's called date and time, not with the end, but just date and date time. Once you have imported the date time package, you would be able to utilize the different functions and utilities which are available in daytime. So let's say I want to get the current date time. In order to do that, let's start with writing current date equals date dot, datetime. And with that, we're getting the current date variable. Okay, so let's try printing out the value. So I'm going to say current date in the print statement. now that we have written the program let's try running it so by clicking on that file and clicking on the run Python file in terminal and when I do that I get the full daytime output over here in terminal nice so let's say we want to format the date time or we want to print only specific value so in this example let's say I want to print year month and date but in a different format so here I'm going to use formatted string and in that I will use curly brackets and to start with I will print let's say the day which is date and after that I will put slash over here and current date dot let's say month and the last thing that I would like to add is here now let's try running this Okay, I think I may have made a mistake over here. It should be day, not date, yes. I click, run, and we get the proper output as expected. So this is how you can pick different daytime pieces or components and then use it. But let's say you want more values. So for that, you can use different daytime formators which are built in. Because the daytime that we are seeing right now, it is in. very long format so for that there is an inbuilt function we will use that it's called string format time string format time so that's SDRF time so in that you can specify the formator and you can see in the pop-up also it's giving you couple of examples you can specify the formats so we can directly pick the one that we want so I'm going to pick this one so output for both of them would be more or less same, right click, run, and we're getting, yeah, okay, not exactly the same because you can see it's having zero as a prefix over here in the output. Nice, so this is how you can format. There are different ways, so let's say if I go to File 20 in that you will find several formats over here, and in that, you can try anything that you want. so let's say I'm not going to right now build the one that I want but you can always poke around and try different things so capital H for getting hours in 24 hour format and capital I for AMPM format I believe and to print AMPM you have to use percentage P so yeah feel free to check out this file which I've added in the repository which is public and if you don't know where to find the repository go to the description section and click on the link for the GitHub. With that you'll be able to see the full code base, which I keep publishing on day-to-day basis. So this is how basically you can print it. But how about I want to, let's say, create a date from the different values which I have? So let's say for the year, I have 2026. For the month, I have 07 as a month, and date I have 26 or let's say 10. Now I want to create a date out of this. So how do we do that? So date time, and then date, time. So the moment I hover my cursor on this function, you can see that in here you can supply year, and then month, and then day, and the other ones are optional in which you can specify hour, minute, second, millisecond, and time zone info as well. But for now, in this example, we're going to specify these three variables, which we have defined here. For your exercise, you can try supplying any of this and let me know what you have tried and what didn't work as expected in case if nothing works out for you. All right, so day time and in that, let's supply here, here equals year, one second, here, month, and then day. So with that, we have created my date, so I'm preserving the value or say getting the result of the function, or constructor and then putting it in my date variable now we can simply print the date if you want to see the output let's try running this this one and with that we are getting the output for that date so yeah this is it with this either you can like you know print the value of current date time or you can use the existing variables in which you're holding the value in form that you can create a date and we also learned about using shtrf time function for formatting the date given date and with that you can get the desired output wherever you want for now we are putting the output in the console so yes that's it for this video see in the next one thank you so much for watching don't forget to hit the like button and subscribe button