Episode: 1182 Title: HPR1182: LiTS 023: Date Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr1182/hpr1182.mp3 Transcribed: 2025-10-17 21:08:31 --- Welcome to Linux in the Shell, episode 23, the date command. My name is Dan Washco, I'll be your host today, and before starting, I'd like to thank Hacker Public Radio for not only hosting the website, but also the audio files. If you have not already contributed or listened to Hacker Public Radio, I strongly suggest to do so, it's a fantastic community. So head on over to HackerPublicRadio.org to find out what you can do and the wonderful shows that are already available for your listening, pleasure, and education. Today we're going to talk about the date command, which is a extremely versatile tool, particularly for getting timestamps and displaying date and other types of information. I use it all the time in scripts to not only, like I say, set a timestamp, but to do some comparisons and stuff like that in there, it's extremely useful. Now the date command makes use of the LC underscore time value of your locale. Now the locale is set when you install a system, when it asks you for your time zone and your keyboard, your language defaults, and stuff like that. That is generally what sets up your locale. So LC time or what date uses for the time formatting is part of locale. Locale basically sets your language and cultural formatting for things like date, time, currency and all that. Now, how you adjust locale and where those settings are stored, a little beyond the scope of this episode, but in a nutshell, again, on ArchSystem, it's in the Etsy.locale.com file. On a Red Hat based system, that information is stored in Etsy, SlashSysConfig, Slash I18, and on a Debian based system, it's an Etsy default slash locale. Look at your documentation for your distribution if you really want to know how to set your locale or reset it or temporarily set your locale for whatever purposes that you would want to. But anyway, the date command uses the LC underscore time values of your locale. Just issuing the date command by itself. Now, I'm going to preface by saying a lot of the examples on the website, maybe some of the things I'll express here in the video, the LC time value is EN underscore us.utf-8. So it's an, I'm coming from a US English perspective here and my time zone is America Slash New York. So it's Eastern time zone with the elite savings time. So be aware of that. Your mileage may vary if you're in a different time zone, different part of the world. So be aware of that different country. Okay, anyway, issuing the date command by itself is just going to display the current date and time of your system. The value is going to come out looking like something like Saturday, February 9th, 1148, 51, time zone being Eastern and a year 2013. So that's kind of the default format on my system when I issue the date command. Now, let's say that you want to find a different time instead of the current time. And then you might say, well, if I already know the time that I'm looking for, why would I want to find it? Well, let's come at it from a different perspective. The dash D option or dash date equals takes a date as a as a string format and returns the value. And you say, well, okay, but what would I put in there? Well, you don't necessarily just put in like, you don't put in April 14th, 2013. And it doesn't just spit back April 14th, 2013. And all actuality, it would, but it would add some other information on there, like probably the hour, which would be set to midnight, 0, 0, 0, 0, 0, minute, 0, 0 seconds, whatever. But anyway, what you would want to do a lot of times with the dash D option is to say like date dash D five days. And that will tell you the date five days from now, that full format, it would set the option to be, you know, just like the the format that we talked about right now before. And the actual syntax would have been date five days. So it would look at the current date of the system and just advance it five days and give you that information. You can do date two days ago. And it would show you those the date two days ago. Again, you might be saying, okay, that starts to make some sense there. I can get some pretty good information if I had that in the script. So you can do a bunch of different options in there, passing there. You can do like days, months, years, hours, minutes, seconds. You can put a go in there. It'll show you the past. So if you did like date dash D five minutes, it would show you five minutes in the future or five months. It would show you five months into the future with the date would be five months in the future. Now that's going to come in real handy when we start talking in a few minutes about different ways to format the date because you can pull some really great information out of there. Like you can get the day of the week of the date five days from now or five months from now, this day of the month, five days from now, what day of the week it's going to be. Or you can look back and say, this day in 2005, seven years ago, no, it'd be eight years ago now, sorry, print out the day of the week it was. So we'll get to that in just a minute. But before, before we continue on, the dash F or dash dash file equals option, what that does is it allows you to do the same thing as dash D, but it takes as input a file that in each line has those parameters in it. So for instance, if you had a file that's called date file and the first line was, you know, five days ago, second line was one month and the third line was two hours, 10 minutes, and the fourth option was like Monday. It would pass to the date command those values. So it would show you the date value three, you know, five days in the past, one month in the future, two hours, 10 minutes in the future, it would show you the date of the next following Sunday coming up or Monday, what I said right there. So you can do like those parameters that you would pass with the dash D, you can pass those, you know, put those into a file as many as you want and pass the dash F command and that file name and it would spit out the values for each line. So that could be pretty handy. So that there's a pretty neat options right there. Let's talk about formatting the output of the date command. Now I could get really, really dry and boring here. Maybe if some of you haven't about this might be a little boring already, but how could it be? This is exciting stuff Linux in the shell. There are a ton of different ways to display different values of the dates, but the format is the same. It execute the date command and if you want to get just specific values of the date or time, it's the plus sign followed by a percentage and a variable. And the variables are all like single letter variables, both upper and lower case, and there's a ton of them. But for instance, if you wanted to get to show just this the full year, the full four year string, that's four characters of the year string, that would be date plus percent capital Y. If you wanted to just look at the name of the day of the full day of the week, that would be date plus percent capital A. So there's a whole bunch of those different things in there. And as you can see, you can start stringing those values together because the formatting in this is that it accepts other characters in there, but it translates the percentage and the variable into what that value for the date command is. So like if you did date plus percent capital Y, dash percent M, let's lower case M, dash percent D, that's going to give you the full date format of percent capital Y is equal to 2013, this is the year, puts a dash in there, then it gives you the month, then puts a dash in there and gives you the year. So if you wanted to actually print out a string, like today is the day of the week, day name of the week, it would be plus, you would put in double quotes, double quotes plus, well you wouldn't put plus, double quotes, today is plus percent capital A, space of the week, and the double quotes, and that would print out whatever it is, like for instance, right now, recording this, today is Saturday of the week, that doesn't make sense, but you get the idea right there. So you can format now, let's go back a second there to when I was talking about the dash D command, okay, when I said the dash D, like I said, well, let's say that you wanted to find the day, this like today, today is Saturday, February, 10th, 2013, what was the day of the week, a year ago, what was the day of the week? Well, if you would put in, if you would type in for the day command, date, and then dash D one year ago, that's going to give you this day a year ago, February 9th, a year ago, and that would be putting it in February 9th, a year ago, that would be Thursday, the week, but I could just get the day of the week by putting percent A, so I would do date, dash D in double quotes one year ago, close the double quotes and do plus percent A, and all I'm getting back is Thursday, the full string, so you can get some really cool stuff. Now, there are some values that aren't necessarily displayed in the default output of the date command, or they're displayed in different ways, you can get, and so you can have alter those values to come back when you do a dash D option. It's really handy, so you can look ahead into the future and say, what's it going to be, what's today's date going to be a year from now? Well, it's not going to be Saturday, a year from now, the date is going to be a year from now, Sunday, so it's just going to be one day ahead, really cool stuff, really fun, great stuff with the date command. If you want to know all those different variables, like percent capital C is going to give you the century that we're in, head on over to the website, look it up, or just go to mandate, that's going to tell you all that information. Now, there are some other options to date that you probably have other formatting options. By default, the date command pads empty spaces with zero, so if you had a single digit month day hour, it would pre-pend to zero to that, like 1 p.m. would be 0 1 p.m. by default. You can change those values, or how that that behavior, with a few formatting options, flags that go in between percent, sign, and the variable, like what we're talking about, we'll start with just talking about minute. Now, minute is, minute of the day is percent capital M, and it has a leading zero. If you put a percent hyphen capital M, then that removes the padded zero, so it gets rid of the zero, it does not do any padding. If instead of a hyphen, you put an underscore in there, it's a big percent underscore capital M. It changes the zero paddings with spaces, so what was before zero nine now becomes space nine. If you put a zero in between value percent zero capital M, you get padded with zero, which is the default we're talking about. Now, there's another two options here. One is the carrot, which is typically that symbol above six, which you do is plus percent carrot, and let's choose a different one. Percent P shows AM or PMs capitalized. Percent capital P shows AM or PM, but lowercase. Well, if you did plus percent carrot capital P, that's equivalent to doing plus percent P, because the carrot says use uppercase if possible, so it would change the lowercase AM and the percent capital P to uppercase. There was the same thing with the hash or the pound sign. That uses lowercase if possible, so that would change percent lowercase P, which is capital AM or PM. If you did percent pound lowercase P, it would change it from capital to lowercase. There's a couple of things there. Now, there's there's an option also to append a number. If you append a number between a percent and the variable that you're using, it's will specify the width of the field. In my playing around with this and experience, the appending, like if you did a plus percent 8Y capital Y, that's going to append eight fields in front of the four digit year. Okay, so you're going to get by default eight zeros in front of the year. Now, if you were to do plus percent 2Y, it does not limit it to only the two characters. It doesn't truncate values. I've not found that the truncate values. It will only pad if the value is greater than the fields being returned, the number of fields being returned. Just be aware of that. There are some other options that are only really supported by specific locales. One is a debt as capital E. So if you were to put a percent capital E and any one of these following variables, lower uppercase C, lower uppercase X, lower uppercase Y, it will use an alternative representation for the values. Now, the main file gives an example of using it in like Japanese locale where it would show you and using with the year, it would show you the year from the emperor's reign, something like that. I don't have any alternate values that I can see on my locale. Similarly, with putting zero, which can only be applied to numeric conversions, if you do a percent zero and the value, it'll use in a locale's alternative if numeric system if you have one. I have not had anything like that for me, so just to be aware of that. All right, so we've covered a lot of basic uses of what you would use the date command, how you can format variables. There are a few other flags there that you can use for specifically displaying the output of the date command and specific ISOs or RFC formats. One is the dash capital R or dash dash RFC dash 2822 and that will output the format in the RFC dash 2822 format, which by default, my system gives returns a Saturday comma zero nine fed uses fed the abbreviation of the month. Then it gives the year 2013 and the time hour, minute, second and then also displays the time numeric time zone value, which is negative zero five zero zero. There are two other options, dash capital I or dash dash ISO dash 8601. That one by default just displays the date in the format of four to two year dash month dash day. Then there's also RFC dash 3339 or dash dash RFC dash 3339. Now be aware that that one requires a time spec to be passed to it. Now both the dash I and dash RFC dash 3339. Dash I will accept the time spec, but it's not required. RFC dash 3339 requires a time spec and what the what a time spec is is it differs between the two because they accept different time specs, but it can be one of these for both ISO 80601 and RFC 3339 except date has time spec just a word date and it returns that date like it both return the year dash month dash day. Then ISO 8601 accepts hours and minutes and what those two those do is that will print for hours. It will print the date and then T for time, capital T and then the hour and then the time zone offset and if you do minutes it will do the year capital T hour colon minutes and then the time zone offset. Now both RFC dash 3339 and ISO 8601 except seconds and that does the formatting of the year will the date the year month day T hour, minute, second and time zone offset. Now the difference between the two is ISO 80601 shows time zone offset as being like 0 5 0 0 whereas RFC dash 3339 shows the offset as as a you know minus 0 5 colon 0 0. And then the final option to both of those is NS for nanoseconds which shows you that the exact same string I was talking about before but after seconds it has a comma and puts the nanoseconds value in there. That's typically a rather large string right there and shows you the nanoseconds that it's at. There is a way to display universal or UTC time and that's a date dash U or dash dash UTC or dash dash universal that will output the the current time and UTC that can be very handy and that covers it for displaying the dates. The date command also allows you to set your systems date or time with the dash S option and if you do date dash S and you specify a time like for instance dash date S in double quotes like 2013 dash 0 1 dash 15 and then 0 8 colon 15 colon 44 that's going to set the current system date to the year 2013 to the month of January to the day of January 15. That would become the date then the time is going to be 8 15 am 44 seconds. Now you have to have elevated or root privileges to be able to adjust your systems date and time. And if you're using something like NTP and network time protocol you probably don't want to be messing around with your date but you can you can't adjust your date. If you leave off any values they just get set to 0. So if you just specify the date it would set the time to 0 0 0 0 0 as if it was midnight stroke of midnight. The final switch that I want to make reference to is called the reference switch. That is dash R or dash dash reference equals and some file name and what that'll do is it will allow you to display the last modification time of a file and then you can use the formatting variables to display that value any format that you want to. So that's again that's the dash R or dash dash reference equals file and displays the last modification time of that file. That's the date command in a nutshell describing it. It's very powerful to wonderful to have. I strongly recommend you get a little comfortable with it because it'll help you with scripts and doing backups and all sorts of nasty things always using the date command. Head on over to the website linux and the shell.org check out the right up on it to get more information on using the date command and the watch the video on some of the examples. My name has been Dan Washco. I thank you for listening to Linux and the shell. I strongly encourage you to support Hacker Public Radio that supports this show and you have a great day. You have been listening to Hacker Public Radio at Hacker Public Radio. We are a community podcast network that releases shows every weekday Monday through Friday. Today's show, like all our shows, was contributed by a HPR listener like yourself. If you ever consider recording a podcast then visit our website to find out how easy it really is. Hacker Public Radio was founded by the Digital Dark Pound and the Infonomicom Computer Club. HPR is funded by the Binary Revolution at binrev.com. All binrev projects are crowd-responsive by lunar pages. From shared hosting to custom private clouds, go to lunarpages.com for all your hosting needs. Unless otherwise stasis, today's show is released under a creative comments, attribution, share a life, lead us all license.