 And there's my thumbnail for this video. Today we're gonna be talking about properly formatting dates. It sounds boring, but it's actually really interesting. Stick with me because we're gonna be working with dates and date formats for a couple of videos, but then we're gonna be using them for useful things in programs. But today we're gonna be looking at taking dates that can be entered by the user in different formats and formatting them in the format you need for whatever programming thing you're doing. Let's get started about this way. Okay, most of you are probably familiar with the date command in your shell scripts in Bash or whatever shell you're using. You just type date, and well, it gives you the date. It gives you the day, the day of the month, the month, the year, the time down to the seconds, and your time zone. Great, but you can format it many different ways simply by doing plus, then percent, and a number of different characters. You can do s to get your seconds for your epoch time. Your Unix time stamps. You can do capital Y for the full year. Lowercase Y for just a two-digit year. You can do M, which is your minutes, so it's 53 past the hour right now. A lowercase M will give you the month. Today is the fifth, or today, this month is the fifth month. We can go D, and that will give you the date, the number, so today is 515. And we can also do things like capital, I already did capital Y. A would give you the day of the week in short format. Capital A will give you the full name of the week, and of course you can do C, which is the same as doing nothing at all. Another option would be dash capital I, so it's not plus in percent. It's just a very common standard date format, and it will give you the year, month, and day with dashes in between. Now, you can also format multiple of these together. So let's go ahead and just clear the screen. I'm gonna say dash again, plus percent, and let's say I want it to be the month. I can do capital, or lowercase M, right? And then I can do forward slash percent, and I can do D for day. And then I can do forward slash percent Y for year, or capital Y for the full four digit. Now, we can also do other characters. We don't want to do dashes, or forward slashes, we can do dashes, but again, if we do this in this format, dash percent M, dash percent D, that's basically the same as doing dash, dash I. Okay, so we're getting lots of different formats here. You also can do basically whatever characters you want. You have your percent and your character there, but I can do periods if for some reason I want to do dots in between, but you also do full words. Now, you can have spaces if you use backslash to indicate them. So I can do month percent, and I can do M, you know, something like that. And then I can do backslash space, and I can say day, and I can do like so, and I can do another backslash space, and I can say year, and I can say percent capital Y, and that will give me the word month, colon, month, day, year. Again, that's great, that's fine and dandy. Lots of different formats there, but what are some of our other options here? Well, we can get the date from the user or input them into our scripts ourselves. So if I say date dash D, I can then give it a date. So I can say like May 10th, 1999, and it will give me the date for that. I can also put that same thing in another format. I can say five slash 10 slash 1999, and it's gonna tell me the same information. I can also say something like 1999 dash 05 dash 10, and it's gonna give me that same date. So you can see that I can write it many different ways. I can also, you know, type something out. I can say Jan 30, 1930, or I can say January 30th, or I can do it with a capital J. It's very forgiving. I mean, you have to type something that makes sense, but the date command is very good at figuring out what date you're talking about. And if you leave out the year, you just give it a day and a month. It's going to assume you're talking about the current year. So there you go, 2021. And I can say 31. And if I do 32, that's gonna give me invalid date because there is no January 32nd. And of course, if you just give it gibberish, it's gonna tell you it's an invalid date. But you can see how there's different formats that you can do the date, but you can get the format you want out of that. So again, if I take any one of these examples I did, I can then just do plus percent, and I can do y slash percent month slash month. Y slash percent day. So there we go, I can format like that. Or if I want dashes, I can do dashes. Or again, if I just want the year, I can have a date inputted and grab the year. You can also grab the day of the week like we talked about earlier, which I think was, was that a, yes. So I can, if I want to know what day of the week that date was, there's Thursday, I can do a capital if I want the full name. So I can give it any date. I can give it any year, I can say 56. I can say 1656, that was a Sunday, January 30th. I can get that. So let's use this to our benefit. We can actually have the user get input. Now, lots of times I've said in the past, if you're gonna let the user input something and it's something predictable, give them a list to choose from. For example, most GUI interfaces, you'll have a calendar and you can have them type in the date, or you can have them pick it from the calendar. That way you know they're picking an actual date. And I would suggest doing the same thing with your bash scripts in many cases. I've used FCF to make menus like that. The date command can help you generate dates that are actual dates. But let's say we want the user to be able to input a date and then we can validate, is this a valid date? If so, continue. Well, I have a script that does just that right here in my folder. So what are we doing here? We're saying, well, this is a bash script. Then we're gonna say read and we're gonna have a prompt. So we're gonna ask a question and whatever the user type is gonna go into a variable called D. So we're gonna say, what is the date of the event? Whatever event we're talking about, right? Now, I'm gonna use the date command dash D. I'm gonna input the variable of whatever they just typed. And then I'm gonna format. I don't think I really need to have that I there. But I'm gonna dump whatever the output is to null. But if it's an invalid date, we're going, it's going to say it's an invalid date. That's part of the date command. And then exit out of the script. It's not gonna continue. If they enter a valid date in one of the many formats the date will recognize, we're then gonna go and get that formatted and we're also gonna get the day of the week. And then we're gonna output to the user. You selected whatever date and in the format that we've set it to. And we're gonna tell what day of the week it is. So I'm gonna go ahead and run that script. I'm gonna say get date. And it says, what's the date of your event? So I can say one, one, 1901. And that was a valid date. So it says you selected. And you can see it's not the format I put it in. It's in the format that my code wants. And then it's gonna say this was a Tuesday. And again, I can write that a different way. I can say Jan, one, 1901. Same thing. I've typed it out in a different format. The code has converted it to whatever format me as the designer of the program wants it in, outputs that to the user and tells you what day of the week it is. Now, again, if I was to give it gibberish, it's gonna, date's gonna say this is invalid command. And then we told it to exit our script. Do not continue. And that can also be true if you put in something that looks like a date, but maybe isn't. Again, we can do, let's say I said 01-33-1901. That's an invalid date. That doesn't exist. So even though it might look like it's a date, if it's not a valid format, or you get like a month invalid. So if I was to say like 13-13-1901, it's gonna tell me that's an invalid date because there's no 13th month. So our script knows to exit. You can also have it loop around and ask them again if that's what you want. So again, this is what my code looks like. Just a couple of lines. Probably could be even shorter, but we're getting the user input. Checking if that's valid. If not, we're going to exit with an exit message of one. Then we're gonna get the date format however I want it. Again, I can go in here and I can change how this is formatted. I'm doing dash I, which gives us the year-month-day, but I can use any of those examples we gave earlier, amongst others. But I can say plus percent month forward slash percent D, percent Y, capital Y. Run that again. If I typed it all right, now again I can say for my month, I can say two or O2. Even if you put extra zeros in there, I can go OO2. I think we'll still work 30-1902 invalidate. OO2 invalidate. So maybe I can't do that. Or there's no 30th of February. That's my problem. OO2, 15, 1902. No, I guess that's an invalidate as well. So maybe I'm wrong about the leading zeros. You can have at least one leading zero. So I can say 15, 1902. Or I can say two, 15, 1902. And of course you can do days in the future. Of course my sentence doesn't make sense here. Was the day of the week. But I can say, again, if you don't give it a year, I can say 10, 10. And it's gonna assume it's talking about this year. So my sentence doesn't make sense Sunday. And Sunday was the day of the week. It's going to be the day of the week at the time of recording this. But I can also say 01, 01, 3000. And that will be a Wednesday. So I hope you found this useful. Again, we're gonna be doing more stuff with dates in the coming videos. And we're gonna use that in some code practically in just two or three videos. But I hope that you're enjoying this. I hope you learned something new. Again, check out the man page for dates. So just man date and man date. And if you scroll down here, it shows you a lot of options that I didn't even go over on formats. So you can do things like, oh, day of the year. That's a percent J. There is one where you can get the day of the week in a numeric value from zero through six. That's handy if you ever want to write a code. Is it before or after Wednesday? You can say if it's this number or higher, do this or this number and lower, do this. You can do nanoseconds. So there's a lot of options in here that I didn't go over. Look through that and find what works out for your project. I do want to thank you again for watching. Visit filmsbychrist.com. That's first of the K. There's a link in the description. I'll probably post this example code on Payspin and put a link to it. So I do thank you for watching. And as always, I hope that you have a great day.