Check out a free preview of the full Introduction to Bash, VIM & Regex course

The "Calendar and Dates" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

James shows how to print out the date with date command as well as showing the month or year with the cal command.

Preview
Close

Transcript from the "Calendar and Dates" Lesson

[00:00:00]
>> James Halliday: So head and tail are pretty handy. There's also one that's a calendar on the command line. I use this all the time as well, cuz I'm pretty much always in the command line, and if you just want to see a really simple calendar, here you go. So we see that it's May 2, 2017, and happily, the 2 is printed with reverse video.

[00:00:24]
So it comes up with a black background, in this case.
>> Speaker 2: Is there a way of changing that when you're doing dark mode because the black background is black?
>> James Halliday: It's reverse video. So that means that if you have a black background it should show up as white, and if you have a white background it should show up as black.

[00:00:41]
>> Speaker 2: I think there's something with Mac again.
>> James Halliday: Macs, you're weird. I don't know anything about Macs. I can't help you there.
>> Speaker 2: Noted.
>> James Halliday: [LAUGH] But also, you can do things like if you want to see all of 2017, there we are. Whole calendar to play with.

[00:00:59]
You can pipe that into head -n5, and the first five lines of the output of the calendar command, it's kind of handy. You can also do things like, if you wanna see October,
>> James Halliday: I guess you have to say oct 2017. You have to give it the year as well, but there we go.

[00:01:20]
Also another handy thing is cal -3, will print out the previous month, current month, and the next month. That's the one that I use all the time, it's super handy.
>> James Halliday: Just a fun thing. There's also the date command. If you type date, it will print out the current time and date, and the date command is really fun, cuz it has these things called format strings.

[00:01:50]
This goes out to a system call. If you remember from at the beginning, we were talking about how the kernel works to get different date parameters, and there's something in POSIX that's called STRF time, and STRF time specifies this little language for doing things like formatting dates. So if we want just the hours and minutes we can do %H is hours, and %M is minutes, it's 13:13 I guess.

[00:02:25]
That should be 13:14 by now. No, it's still 13:13. So we can do %H:%M:%S and every one of those percents is replaced by hours, minutes, seconds, in this case. There's also one for the year, the full year, 2017. There's also one for the month, which is the lowercase m, and the day is d, and there's a shortcut for a few of these things like the year, month, date format like this with dashes.

[00:03:05]
There's a shortcut called %F that gives you that, and there's a shortcut for the other part which is %, I actually forget what it's called. So, we can look at the mandate, and if you scroll down a couple of pages, there's whole section about what all of this presents mean.

[00:03:22]
So, if we see the part with the hours, minutes, seconds, there should be something like, here we go, %X, I think, and %T. Right, so I think %T is a little bit nicer than %X. I don't really care what the locale is, I want it to always look like that.

[00:03:48]
Date is sometimes really handy if you wanna do something with this format strings. If you wanna do something like automatically generate a log file that has the date in it, it's very useful for that, because you can do things like, I'll show in a bit how to use back ticks to use these commands to kind of like create file names in place and that kind of stuff, but we have something like %F, we could do something like echo `.txt, and that bit inside the backticks is substituted for that file.

[00:04:27]
So you can do something like echo hello there appends to a log file that has a name that's based on the current date. So there'll be a new log file every day.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now