date: The possibilities are endless

I had a few technical glitches this morning, if you were wondering why today’s posts were late. Which is a bit suspicious, since these two are technically the last ones I have left to share from the D section. 😐

First up is date, which ranks among some of my favorite command line tools. I saved it to the last mostly because I wanted to show a little more detail than my usual quick glance.

2013-10-17-lv-r1fz6-date

The key to understanding and loving this wonderful gift from coreutils is in the help flags. Most of the fun happens when you add formatting, but to start with, just this

date

will give you the current date and time, according to your machine’s clock.

Change it only slightly, and you get UTC time.

date -u

You can shift that information slightly, into the future or the past, by adding a -d flag and specifying 10 minutes, 2 hours, 8 weeks or what have you.

Formatting is where the fun is though. Here’s just the date, in easy-read year-month-day format.

date +%F

The plus sign triggers the formatting sequence, the percent sign specifies the style. Unless I am mistaken, this is how the date is usually shown in America.

date +%D

In other words, month-slash-day-slash-year. Counterintuitive from my perspective, but hey, who am I to blow against the wind?

Here’s date and time on the same line, with a space separating them.

date +%F\ %H:%M:%S

And here’s the same thing on two different lines. You can jam a newline into date with the %n format.

date +%F%n%H:%M:%S

Now let’s get really crazy. Write it all out like a sentence.

date +"Today is "%A", "%B\ %e", "%Y"."

And the current time.

date +"The time is "%-l:%M\ %p"."

This is fun to me. Stick the two together and you get the date and time all at once. Remember this when you’re fiddling with things like ticker.

date isn’t just for your local machine though. The TZ variable can be fed through date to show time zones outside local.

TZ='Asia/Tokyo' date

Ta-da! Get fancy with that stuff:

TZ='Asia/Tokyo' date +"Japan time is now "%-l:%M\ %p".

Oh, you’re an Internet hero, are you?

TZ='Asia/Tokyo' date +"Japan time is now "%-l:%M\ %p" on "%A", "%B\ %e", "%Y"."

No, I don’t just sit and tinker with date in my free time. Okay, actually, yes, I do. … 😐

If you interject date into other commands, you can arrange files or commands to include the date in specific formats. Ergo,

mv testfile.txt `date +%F-%H:%M:%S`.txt

That’s most often the way I use date; I like to arrange family photos and so forth by date and time. That includes screenshots. 😉

fbgrab -s 5 `date +%F-%H:%M:%S`-${HOSTNAME}-date.png

Sadly, beyond formatting and measuring out arbitrary lengths of time, date can’t really handle much in the way of calculating time. For that I recommend dateutils.

I could go on for a long time, but by now you should get the picture. If you need more examples, check out the examples page straight from the coreutils site, or Wikipedia’s list of examples. The possibilities are endless. …

In the mean time I’m going to dig around and see if I can get that last post from the D section ready again. … 😉

Advertisements

6 thoughts on “date: The possibilities are endless

  1. Pingback: Bonus: And so D goes | Inconsolation

  2. Ian Munsie

    I highly recommend caution when trying to use date for timezone conversion! What would you expect this command to do:

    TZ=madeupland date

    You would expect an error that there is no timezone called madeupland, right? But, that’s not what it does:

    ian@delenn~ [i]> env TZ=madeupland date
    Friday 18 October 02:48:03 madeupland 2013

    So, if you make a typo in the timezone, or you specify a city that doesn’t happen to be the one that date knows about, or you try specifying a damn TIMEZONE instead of a place it will cock up and tell you UTC instead – without letting you know that’s what it’s doing.

    I could tell you how to convert from one arbitrary timezone to another with the date command, but I won’t because it would be irresponsible of me to encourage using date for timezone conversion.

    1. K.Mandla Post author

      That’s interesting. I’m surprised there’s no error checking for that within date, or at least some sort of flag to mention that there’s no corresponding location. Perhaps someone should file a bug report?

  3. Pingback: tcat: Just quickly, because it’s fun | Inconsolation

  4. Pingback: time, time and times: Too much time on my hands | Inconsolation

  5. Pingback: stat: Simple file information, and more | Inconsolation

Comments are closed.