Day/Hour diff between two date

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Day/Hour diff between two date
# 1  
Old 08-04-2015
Day/Hour diff between two date

Dear All

I need to find out day diff between two dates. date -d or date -- day is not working in mine system.

Currently i am using below code but it gives me wrong value while month change.


Date 1: 20150802 11:30:45
Date 2: 20150728 16:30:45

code used:

awk '{t1=$2; t2=$4; split(t1,a,":");split(t2,b,":"); if(($1-$3)){print $0,int(((substr(($1-$3+1),2,1)*24*3600+0*60+0)-(b[1]*3600+b[2]*60+b[3]))/3600+((0*3600+0*60+0)+(a[1]*3600+a[2]*60+a[3]))/3600)} else {print $0,int($1-$3+((a[1]*3600+a[2]*60+a[3])-(b[1]*3600+b[2]*60+b[3]))/3600)}}'

Kindly suggest plz.

Jaydeep Sadaria
# 2  
Old 08-04-2015
I'm no expert with awk, but here's something i'd like to share:

While i do agree that one-liners are cool, there are occasions it just makes no sense.
Saying, a well formated code is easier to read - and find issues/improvements, than within a one liner of that length. (me have FHD (1920x1080) display, and i need to scroll sideways)

This said, you might want to use a function (rather than an if-block) to transform time and date to seconds and to avoid things like:
... +((0*3600+0*60+0)+ ..

For which you could have just written +0.

Once you transformed the date+time to seconds, you simply subtract the one from the other and get the difference.

Hope my 2 cents help
# 3  
Old 08-04-2015
With GNU date:
d1=$(date -d "20150802 11:30:45" +%s)
d2=$(date -d "20150728 16:30:45" +%s)
diff=$(( d1 - d2 )) # This gives the difference between the two dates in seconds.

# 4  
Old 08-04-2015
You having said "... date -d or date -- day is not working in mine system." , what IS working on your system? What's your OS, shell, date version?
# 5  
Old 08-04-2015
Day/Hour diff between two date

Its SunOS 5.10. Only -a and-u option available. Below is op of man date.

Reformatting page.  Please Wait... done

User Commands                                             date(1)

     date - write the date and time

     /usr/bin/date [-u] [ +format]

     /usr/bin/date [ -a  [-]sss.fff]

     /usr/bin/date [-u] [ [mmdd] HHMM |  mmddHHMM [cc] yy]  [.SS]

     /usr/xpg4/bin/date [-u] [ +format]

     /usr/xpg4/bin/date [ -a  [-]sss.fff]

     /usr/xpg4/bin/date [-u] [ [mmdd] HHMM |  mmddHHMM  [cc]  yy]

     The date utility writes the date and time to standard output
     or attempts to set the system date and time. By default, the
     current date and time is written.

     Specifications of native language translations of month  and
     weekday  names  are  supported.  The month and weekday names
     used for a language are based on the locale specified by the
     environment variable LC_TIME. See environ(5).

     The following is the default form for the "C" locale:

     %a %b %e %T %Z %Y

     For example,

     Fri Dec 23 10:10:42 EST 1988

     The following options are supported:

     -a [-]sss.fff   Slowly adjust the time  by  sss.fff  seconds
                     (fff represents fractions of a second). This
                     adjustment can be positive or negative.  The
                     system's  clock  is  sped  up or slowed down
                     until  it  has  drifted  by  the  number  of
                     seconds  specified.  Only the super-user may
                     adjust the time.

     -u              Display (or set) the date in Greenwich  Mean
                     Time  (GMT-universal  time),  bypassing  the
                     normal conversion to (or from) local time.

SunOS 5.10          Last change: 11 May 2004                    1

User Commands                                             date(1)

     The following operands are supported:

     +format         If the argument begins with +, the output of
                     date is the result of passing format and the
                     current time to strftime().  date  uses  the
                     conversion   specifications  listed  on  the
                     strftime(3C) manual page, with  the  conver-
                     sion  specification  for  %C  determined  by
                     whether /usr/bin/date or  /usr/xpg4/bin/date
                     is used:

                     /usr/bin/date           Locale's  date   and
                                             time representation.
                                             This is the  default
                                             output for date.

                     /usr/xpg4/bin/date      Century   (a    year
                                             divided  by  100 and
                                             truncated   to    an
                                             integer)     as    a
                                             decimal number  [00-

                     The string is always terminated with a  NEW-
                     LINE.  An argument containing blanks must be
                     quoted; see the EXAMPLES section.

     mm              Month number

     dd              Day number in the month

     HH              Hour number (24 hour system)

     MM              Minute number

     SS              Second number

SunOS 5.10          Last change: 11 May 2004                    2

User Commands                                             date(1)

     cc              Century (a year divided by 100 and truncated
                     to  an integer) as a decimal number [00-99].
                     For example, cc is 19 for the year 1988  and
                     20 for the year 2007.

     yy              Last two digits of the year number. If  cen-
                     tury  (cc)  is not specified, then values in
                     the range 69-99 shall refer to years 1969 to
                     1999  inclusive,  and  values  in  the range
                     00-68 shall refer to  years  2000  to  2068,

     The month, day, year number, and century may be omitted; the
     current  values  are  applied  as defaults. For example, the
     following entry:

     example% date 10080045

     sets the date to Oct 8, 12:45 a.m. The current year  is  the
     default  because no year is supplied. The system operates in
     GMT. date takes care of the conversion  to  and  from  local
     standard  and  daylight time. Only the super-user may change
     the date. After successfully setting the date and time, date
     displays  the  new date according to the default format. The
     date command uses TZ to  determine  the  correct  time  zone
     information; see environ(5).

     Example 1: Generating Output

     The following command:

     example% date '+DATE: %m/%d/%y%nTIME:%H:%M:%S'

     generates as output

     DATE: 08/01/76

     TIME: 14:45:05

     Example 2: Setting the Current Time

     The following command sets the current time to 12:34:56:

     example# date 1234.56

SunOS 5.10          Last change: 11 May 2004                    3

User Commands                                             date(1)

     Example 3: Setting Another Time and Date in  Greenwich  Mean

     The following command sets the date to  January  1st,  12:30
     am, 2000:

     example# date -u 010100302000

     This is displayed as:

    Thu Jan 01 00:30:00 GMT 2000

     See environ(5) for descriptions of the following environment
     variables  that  affect the execution of date: LANG, LC_ALL,

     TZ       Determine the timezone in which the time  and  date
              are  written, unless the -u option is specified. If
              the TZ variable is not set and the -u is not speci-
              fied, the system default timezone is used.

     The following exit values are returned:

     0        Successful completion.

     >0       An error occurred.

     See attributes(5) for descriptions of the  following  attri-

    |       ATTRIBUTE TYPE        |       ATTRIBUTE VALUE       |
    | Availability                | SUNWcsu                     |
    | CSI                         | enabled                     |


SunOS 5.10          Last change: 11 May 2004                    4

User Commands                                             date(1)

    |       ATTRIBUTE TYPE        |       ATTRIBUTE VALUE       |
    | Availability                | SUNWxcu4                    |
    | CSI                         | enabled                     |
    | Interface Stability         | Standard                    |

     strftime(3C), attributes(5), environ(5), standards(5)

     no permission           You are not the super-user  and  you
                             tried to change the date.

     bad conversion          The  date   set   is   syntactically

# 6  
Old 08-04-2015
Are you sure its the only available date on your system?
find / -name date 2>/dev/zero

2004 seems rather old to me.
# 7  
Old 08-04-2015
If you are allowed, install the GNU utilities
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies

2. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

3. Shell Programming and Scripting

Finding/Grep on files with date and hour in the file name

Hi, I have a folder structure as follows, DATA -> 2012-01-01 -> 00 -> ABC_2012-01-03_00.txt -> 01 -> ABC_2012-01-03_01.txt -> 02 -> ABC_2012-01-03_02.txt ... -> 23 -> ABC_2012-01-03_02.txt -> 2012-01-02 -> 2012-01-03 So the dir DATA contains the above hierarchy, User input Start and... (6 Replies)
Discussion started by: mihirvora16
6 Replies

4. Red Hat

Cron entry for every 10 mints on business day business hour

Could you “crontab” it to run every 10 minutes on work days (Mo - Fr) between 08:00 and 18:00 i know to run every 10 mints but can any one guide me how to achieve the above one (2 Replies)
Discussion started by: venikathir
2 Replies

5. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

6. Shell Programming and Scripting

Generate last hour date in YYYYmmddHH

can some one show me how to generate last hour of date for example: 2012010100 -> 2011123123 // check first date of a new year 2008030100 -> 2008022923 // check leap year as well And also no perl code is restricted. This is what i am facing issue now, can someone provides help to me. (4 Replies)
Discussion started by: alvin0618
4 Replies

7. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

8. Shell Programming and Scripting

Compare file timestamp with current date. Diff must be 1 hour.

Hello, I've created the script below to compare the content of two files with a delay of an hour. After an hour, the lines that exist in both files, will be printed and executed. The script now uses a counter to countdown 50 minutes. But what I would prefer is to check the file timestamp of... (3 Replies)
Discussion started by: taipan
3 Replies

9. Shell Programming and Scripting

Unix Script for getting date and validating just Hour

Hi, Can someone guide me to write a unix script for getting a hour out of a date command and validating hour to see if its > 7 and < 16. if hours is >7 and <16 then assign a variable value of 0730 and if hour is >16 then assign a variable value of 1630? Help appreciated. Thanks in advance.... (9 Replies)
Discussion started by: zulfikarmd
9 Replies

10. Shell Programming and Scripting

Get date and time for past 1 hour from current date

Hi, I need to get the date and time for past 1 hour from the current date. Anyone know how to do so? Thanks (5 Replies)
Discussion started by: spch2o
5 Replies
Login or Register to Ask a Question

Featured Tech Videos