Date Arithmetic In Linux Shell Scripts
How to write a Linux shell scripts that backup previous month data files on monthly basis? Of course, it involves date time calculation! Unless you’re using GNU Coreutils / GNU date, doing date arithmetic in shell scripts will not be that straight-forward as a novice think!
I just can’t recall that SCO Unix and Concurrent Real Time Unix (RTU) have GNU date installed, when I was exposed to Unix for the first time in year 2000. But, I believe that GNU Coreutils is a standard package that available on most Linux distributions as of today.
How to do date arithmetic in Linux shell scripts using GNU date command?
GNU date is one of the many common Linux programs in GNU Coreutils package. Here are some of the examples of performing date time arithmetic with GNU date command, using
Isn’t it easier for date time calculation related shell scripts with GNU date installed? As said just now, GNU date is not a standard command in Unix variants.
In case GNU date is not available, you might need to write a rather long shell scripts to accommodate some simple date time calculations. Potentially, it could end up as a messy or hard to maintain shell scripts!
In case GNU Coreutils / GNU Date is not installed, you can write a simple C program in few lines of source code to satisfy the date arithmetic in shell scripts! The compiled C program is highly portable among Unix / Linux variants, as the date arithmetic library (time.h) is a standard C library!
How to write and compile a simple date arithmetic C program?
To compile the C program source code using GCC compiler, the general syntax for these simple source code is
For C/C++ tutorial or standard C/C++ library reference, I’m referring to cplusplus.com
Calculate the nth-day from today
Return the literal date of a given Unix Epoch time
To convert a give literal date to Unix Epoch Time (total seconds elapsed since midnight of 1970-01-01)

I just can’t recall that SCO Unix and Concurrent Real Time Unix (RTU) have GNU date installed, when I was exposed to Unix for the first time in year 2000. But, I believe that GNU Coreutils is a standard package that available on most Linux distributions as of today.
GNU date is one of the many common Linux programs in GNU Coreutils package. Here are some of the examples of performing date time arithmetic with GNU date command, using
-d or --date option switch:date -d "last month" +%mdate -d "2 month ago" +%bdate -d "-2 month" +%Bdate -d "-1 hours" +%Hdate -d "tomorrow"date -d "yesterday" +"%d %B %y"date -d "last week" +%adate -d "$(date +%Y-%m-15) -1 month" +'Last month was %B!'date -d "1970-01-01 00:00:01 UTC" +%sdate -d "1970-01-01 1234567890 sec" +"%F %T %z"
- Return as last month in numeric format
- Return as last month in abbreviated month name
- Return as last month in full month name
- Returns previous hour in 24-hours format (00-23)
- Return the date and time of tomorrow
- Return the date of yesterday in dd MMM yy
- Return last week in abbreviated weekday name
- To avoid getting result of “July” when executing
date -d "-1 month" +%B on 31 July 2006, because “2006-07-31 -1 month” might evaluate to 2006-07-01, because 2006-06-31 is an invalid date!- Return the Unix Epoch time (the total seconds elapse since midnight of 1970-01-01), which is 1 secs in this case.
- Return the literal date format of the given Unix Epoch time.
Isn’t it easier for date time calculation related shell scripts with GNU date installed? As said just now, GNU date is not a standard command in Unix variants.
In case GNU date is not available, you might need to write a rather long shell scripts to accommodate some simple date time calculations. Potentially, it could end up as a messy or hard to maintain shell scripts!
In case GNU Coreutils / GNU Date is not installed, you can write a simple C program in few lines of source code to satisfy the date arithmetic in shell scripts! The compiled C program is highly portable among Unix / Linux variants, as the date arithmetic library (time.h) is a standard C library!
How to write and compile a simple date arithmetic C program?
To compile the C program source code using GCC compiler, the general syntax for these simple source code is
gcc -o program_name program_source_code.cFor C/C++ tutorial or standard C/C++ library reference, I’m referring to cplusplus.com
Calculate the nth-day from today
#include <stdio.h>
#include <time.h>
#define c_DaySecs 86400
int main (int argc, char ** argv)
{
int Day;
time_t TotSec;
struct tm * TimeStruct;
if ( argc != 2 )
{
printf("Syntax\n\t%s nth-Day\n",argv[0]);
return 1;
}
else
{
Day = atoi( argv[1] );
TotSec = time( NULL );
TotSec = TotSec + ( Day * c_DaySecs );
TimeStruct = localtime( &TotSec );
printf("%i days is %s",Day,ctime(&TotSec));
printf("%i days is %i-%i-%i\n", \
Day, TimeStruct->tm_mday, \
TimeStruct->tm_mon + 1, \
TimeStruct->tm_year + 1900 );
TimeStruct = NULL;
return 0;
}
}
Return the literal date of a given Unix Epoch time
#include <stdio.h>
#include <time.h>
int main (int argc, char ** argv)
{
time_t TimeNow;
struct tm * TimeStruct;
if ( argc < 2 || argc > 3)
{
printf("Syntax\n\t%s SEC FLAG\n",argv[0]);
return 1;
}
else
{
TimeNow = atoi( argv[1] );
if (argv[2] == NULL || atoi( argv[2] ) == 0)
{
TimeStruct = localtime( &TimeNow );
printf( "%s in Local Timezone is %s", \
argv[1], asctime( TimeStruct ) );
}
else
{
TimeStruct = gmtime( &TimeNow );
printf( "%s in GMT is %s", \
argv[1], asctime( TimeStruct ) );
}
return 0;
}
}
To convert a give literal date to Unix Epoch Time (total seconds elapsed since midnight of 1970-01-01)
#include <stdio.h>
#include <time.h>
int main (int argc, char ** argv)
{
int DD, MM, YYYY = 0;
time_t TimeNow;
struct tm * TimeStruct;
if ( argc != 4 )
{
printf("Syntax\n\t%s DD MM YYYY\n",argv[0]);
return 1;
}
else
{
DD = atoi( argv[1] );
MM = atoi( argv[2] );
YYYY = atoi( argv[3] );
TimeNow = time( NULL );
TimeStruct = localtime( &TimeNow );
TimeStruct->tm_year = YYYY - 1900;
TimeStruct->tm_mon = MM - 1;
TimeStruct->tm_mday = DD;
TimeNow = mktime( TimeStruct );
printf( "%ld\n", TimeNow );
return 0;
}
}
Custom Search







2010 •
[...] these glitches perfectly!. Knowing that limitation, but without Googling for a trick, I posted a Date-Time calculation C program source code, with the first method! Imagine how stupid I was, to replace tonnes of in the C program [...]
[...] to different way of handling text file encoding, a Linux shell scripts might not working after transferring from one OS to another, in particularly from Windows to [...]
[...] files in binary form or bit level (i.e. 0 and 1), fix Bad Interpreter error message when executing Linux Bash shell scripts, [...]
[...] mentioned about date time calculation shell script in an earlier post. Though, the GNU date examples related to Unix Epoch time (a.k.a. Unix time, [...]
thank you for your examples, I just noticed that :D
Thank you very much, it is helpful resouce.
Thank you very much for having shared this. It was something that I needed to do in a hurry, and your post really saved my day.
Keep up the good work.
Best regards,
Marcus