Visit Our UNIX and Linux User Community


Simple date and time calulation in BASH


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Answers to Frequently Asked Questions Tips and Tutorials Simple date and time calulation in BASH
# 1  
Old 10-08-2006
Simple date and time calulation in BASH

The GNU date command in full of goodies but not when it comes to calculate a date or time difference. Here is what I came up with after looking to more than one solution.

Code should be self explaining.

Code:
#!/bin/bash

date2stamp () {
    date --utc --date "$1" +%s
}

stamp2date (){
    date --utc --date "1970-01-01 $1 sec" "+%Y-%m-%d %T"
}

dateDiff (){
    case $1 in
        -s)   sec=1;      shift;;
        -m)   sec=60;     shift;;
        -h)   sec=3600;   shift;;
        -d)   sec=86400;  shift;;
        *)    sec=86400;;
    esac
    dte1=$(date2stamp $1)
    dte2=$(date2stamp $2)
    diffSec=$((dte2-dte1))
    if ((diffSec < 0)); then abs=-1; else abs=1; fi
    echo $((diffSec/sec*abs))
}

# USAGE # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

# convert a date into a UNIX timestamp
    stamp=$(date2stamp "2006-10-01 15:00")
    echo $stamp

# from timestamp to date
    stamp2date $stamp

# calculate the number of days between 2 dates
    # -s in sec. | -m in min. | -h in hours  | -d in days (default)
    dateDiff -s "2006-10-01" "2006-10-32"
    dateDiff -m "2006-10-01" "2006-10-32"
    dateDiff -h "2006-10-01" "2006-10-32"
    dateDiff -d "2006-10-01" "2006-10-32"
    dateDiff  "2006-10-01" "2006-10-32"

# number of seconds between two times
    dateDiff -s "17:55" "23:15:07"
    dateDiff -m "17:55" "23:15:07"
    dateDiff -h "17:55" "23:15:07"

# number of minutes from now until the end of the year
    dateDiff -m "now" "2006-12-31 24:00:00 CEST"

# Other standard goodies from GNU date not too well documented in the man pages
    # assign a value to the variable dte for the examples below
    dte="2006-10-01 06:55:55"
    echo $dte

    # add 2 days, one hour and 5 sec to any date
    date --date "$dte  2 days 1 hour 5 sec"

    # substract from any date
    date --date "$dte 3 days 5 hours 10 sec ago"
    date --date "$dte -3 days -5 hours -10 sec"

    # or any mix of +/-. What will be the date in 3 months less 5 days
    date --date "now +3 months -5 days"

    # time conversions into ISO-8601 format (RFC-3339 internet recommended format)
    date --date "sun oct 1 5:45:02PM" +%FT%T%z
    date --iso-8601=seconds --date "sun oct 1 5:45:02PM"
    date --iso-8601=minutes

    # time conversions into RFC-822 format
    date --rfc-822 --date "sun oct 1 5:45:02PM"

Warning: the non-standard format %s might not work on all version of date. Works fine of my GNU date 5.2.1

Last edited by ripat; 10-09-2006 at 12:57 PM.. Reason: Added some options to the function dateDiff
These 7 Users Gave Thanks to ripat For This Post:

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add missing date and time in a bash script?

Hi Again, I have a file that contains date and time for the past 2 hours. What i need is add missing date and time in a file. INPUT 2016-01-13 01:33 10 2016-01-13 01:31 10 2016-01-13 01:30 10 2016-01-13 01:29 10 2016-01-13 01:28 10 2016-01-13 01:27 10 2016-01-13 01:26 10 2016-01-13... (14 Replies)
Discussion started by: ernesto
14 Replies

2. Shell Programming and Scripting

How to do simple date (time) calculation in shell script?

Hi, I'm looking for a way to do a simple math calc during a shell script as a means of logging how long a particular task takes. For example... STARTTIME=whenever this script starts ./path/to/command.sh >>logfile.log TOTALTIME=<time at this stage of the script after above command... (7 Replies)
Discussion started by: nbsparks
7 Replies

3. Shell Programming and Scripting

Complex calulation in shell script

Hi , I need one help to do some complex calculation in shell script. here is what i need to do:- a=2 b=2 c=2 d=2 result=a+(b/(20*c))+(c/(10*d)) is there any thing special there so that i can group intermdiate results. Please help me if you have any idea. (4 Replies)
Discussion started by: harpal singh
4 Replies

4. Shell Programming and Scripting

Simple date and time calulation in BASH

There is a closed Thread: <url>Here will be the url to the original post once I have 5 posts in this forum...</url> But a small bug had found his way into this very cool and simple code. #!/bin/bash date2stamp () { date --utc --date "$1" +%s } stamp2date (){ date --utc --date... (2 Replies)
Discussion started by: frood
2 Replies

5. Shell Programming and Scripting

date time stamps in bash

I'm looking for a way to have the "date" command output the date in a specific format. I'm not familiar with the different ways to use the date command at all. i read up on it, but i dont get how to manipulate it. i know that i can get the date format to give me a format like: 2012-10-13... (6 Replies)
Discussion started by: SkySmart
6 Replies

6. Solaris

modifying date and time and time zone on solaris 5.10 with (redundant server) veritas

I have a cluster of two Solaris server (veritas cluster). one working and the other is standby I am going to change the date on them , and am looking for a secure solution as it is giving an important service. my opinion is that the active one doesn't need to be restarted (if I don't change the... (1 Reply)
Discussion started by: barry1946
1 Replies

7. Shell Programming and Scripting

Converting date/time and generating offsets in bash script

Hi all, I need a script to do some date/time conversion. It should take as an input a particular time. It should then generates a series of offsets, in both hour:minute form and number of milliseconds elapsed. For 03:00, for example, it should give back 04:02:07 (3727000ms*) 05:04:14... (2 Replies)
Discussion started by: emdan
2 Replies

8. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

9. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

10. HP-UX

a simple way of converting a date in seconds to normal date

Hi all! I'm working on a HPUX system, and I was wondering if there is a simple way to convert a date from seconds (since 1970) to a normal date. Thanks (2 Replies)
Discussion started by: travian
2 Replies

Featured Tech Videos