Today I was working on a script, and one of the subroutines needed simple seconds-based arithmetics with time. As you probably remember from my date and time in Unix scripts article, the easiest way to approach this task is to deal with the raw representation of date and time in Unix – the Unix epoch times. This post will show you how to convert standard dates into Unix epoch times in Perl.
date
Confirm the Day of the Week Based on a Timestamp
I recently created a Unix Questions and Answers page, if you have a Unix question – feel free to ask it there using the submit form and I'll do my best to help you out.
Today's Unix question is this:
How can we write a shell script in unix to find the day of the week when date is given?
The solution for this is even simpler: there's no need for Unix scripting, all you need is to have GNU date command at your disposal. I've already shown you all the basic date/time calculations using this great tool, and that's just another way of using it.
How to find a Day of the week based on timestamp
All you need is to know the base date. Let's say I'm interested in October 16th, 2009. Here's how easy it is to confirm that day will be Friday:
ubuntu$ date -d "Oct 16 2009" "+%a" Fri
That's it – enjoy!
See also:
Unix Scripting: Time and Date
If you have followed this blog for a while, you should remember how to use variables in Unix shell scripts.
Going further, I'd like to show you some basics of working with time and date in your scripts – generating all sorts of timestamps and timing some parts of your script for reporting purposes.