I have two time stamps one is like this 24MAR17:00:14:09 and another is like this 2017-03-25 05:52:09.373941000 and i want to compare the two time stamps same or not i tried a lot date function and awk function but nothing giving me results please help me.

  • what is your comparison? – RomanPerekhrest Jul 7 '17 at 7:12
  • I want to compare those two time stamps whether they are same or not – Rahul Jul 7 '17 at 7:20
  • 1
    Those timestamps cannot be equal because they have different precision. Or are you only interested in a comparison to the second? Nearest second? Truncated to the current second? – roaima Jul 7 '17 at 8:41

bash solution:

d2='2017-03-25 05:52:09.373941000'

d1="$(sed -E 's/^([0-9]{2})([A-Z]{3})([0-9]{2}):/\1-\2-\3 /' <<< $d1)"

if [[ $(date -d "$d1") == $(date -d "$d2") ]]
    echo "dates are equal"
    echo "dates are unequal"

Try this,

The time 2017-03-25 05:52:09.373941000 can be converted to the first format 24MAR17:00:14:09 and then do a comparison.

date_out=`date +%d%b%y:%H:%M:%S -d "2017-03-25 05:52:09.373941000" |  tr '[:lower:]' '[:upper:]'`
[ $date_in == $date_out ] && echo "The dates match"

The main problem is to parse the strings provided. The command date accepts just some formats, not all. The solution is to take advantage of busybox's date ability of accepting (almost) any format.


  1. Create a format string that prints a date in the same format as supplied:

    $ date -u +'%d%b%y:%T'
  2. Use that string to tell busybox date how to parse the date string. Be aware that the local TZ may change the result, use -u (UTC) to avoid mistakes, or make sure to set the correct TZ for the string of time to be parsed.

    $ d1='24MAR17:00:14:09'
    $ busybox date -u -D '%d%b%y:%T' -d "$d1"
    Fri Mar 24 00:14:09 UTC 2017
  3. Make busybox date to print the value in seconds (since epoch):

    $ busybox date -u -D '%d%b%y:%T' -d "$d1" +'%s'


You may repeat the procedure for the second timestamp as well, but, in this case, the string is understood by date directly:

$ d2='2017-03-25 05:52:09.373941000'
$ date -u -d "$d2" +'%s'

And busybox date removing the nanoseconds (${d2%.*}) (more about this later):

$ busybox date -u -d "${d2%.*}" +'%s'


You should have captured results above in two variables:

$ resd1="$(busybox date -u -D '%d%b%y:%T' -d "$d1" +'%s')"
$ resd2="$(date -u -d "$d2" +'%s')"

Then, calculate the difference and compare to zero:

$ (( resd1-resd2 )) && echo "Dates are different" || echo "Dates are equal"
Dates are different

Or directly compare the strings (not the values):

$ [[ "$resd1" == "$resd2" ]] && echo "equal" || echo "different"


There are three issues if you need to compare also nanoseconds.

  1. The first value does not have nanoseconds. The first is that the first date does not have the nanoseconds value.
    You may append 9 zeros:

    $ resd1="$(busybox date -u -D '%d%b%y:%T' -d "$d1" +'%s')"
    $ resd1="$resd1""$(printf '%0*d' 9 0)"
    $ echo "$resd1"echo "$resd2 - $resd1"

    Or (better) re-process the value with date:

    $ resd1="$(date -u -d "@$resd1" +'%s%N')"
    $ echo "$resd1"
  2. The second date has nanoseconds. In this case the second date has a format that date could directly process, but if it had a format that required pre-processing by busybox date, we will need to append the nanoseconds later. As an example:

    $ d2='2017-03-25 05:52:09.373941000'
    $ resd2="$(busybox date -u -D '%Y-%m-%d %T' -d "$d2" +'%s')${d2##*.}"
    $ echo "$resd2"
  3. And, the bash you are running needs to be able to process 64 bit integers.
    If so, calculate the difference:

    $ echo "(( $resd2 - $resd1 ))"
    (( 1490421129373941000 - 1490314449000000000 ))
    $ echo "$(( $resd1 - $resd2 ))"

    Which is a little over 106 thousand seconds (~29 hours):

    $ echo "$(( ($resd2 - $resd1)/10**9 ))"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.