Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: command to to find difference between dates

  1. #1

    Default command to to find difference between dates

    For code i'm writing, I need to find the difference one time/date and another time/date.

    Is there a command to find the difference between one date and another date.

    m1/d1/y1 - m2/m2/y2

    I can figure out on my own, but I'm hoping for a command that does this function for you.


    Need help.

  2. #2
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,587
    Blog Entries
    14

    Default Re: command to to find difference between dates

    Completely depends on the kind of code you're wrighting. MySQL queries can do this, php can do it. I'm oldfashioned, mostly bring dates back to secs from 1970-1-1 0:00:00 than subtract, and format the result.
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  3. #3
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    26,327
    Blog Entries
    15

    Default Re: command to to find difference between dates

    Quote Originally Posted by lord_valarian View Post
    For code i'm writing, I need to find the difference one time/date and another time/date.

    Is there a command to find the difference between one date and another date.

    m1/d1/y1 - m2/m2/y2

    I can figure out on my own, but I'm hoping for a command that does this function for you.


    Need help.
    Hi
    Install dateutils and there is ddiff and dadd... check the man page for formatting as required...

    Code:
    ddiff 2017-11-15 2018-27-08
    350
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  4. #4

    Default Re: command to to find difference between dates

    Hi,

    Since openSUSE has GNU date(1) you can probably convert the dates to epoch and do the math.

    Code:
    date1=$(date -d 'yesterday' '+%s')
    Code:
    date2=$(date -d 'now' '+%s')

    Code:
    echo $(( date2 - date1 ))
    Code:
    86410
    Not sure it helps but GNU date(1) can give some human readable format as well.

    Code:
    date -d 'yesterday' '+%F %T'
    Code:
    date -d '-1 year' '+%F %T'
    Code:
    date -d '-30 min'  '+%F %T'

    Code:
    bc -q <<<  "$(date -d '-30 min' '+%s') -  $(date -d 'yesterday' '+%s')"
    see the man pages for more info.

    Code:
    man 1 date
    If you have control over the date, meaning you're the one giving the date format then yeah GNU date(1) might be a good choice for you, if not well dateutils as suggested above.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  5. #5

    Default Re: command to to find difference between dates

    Code:
    malcolmlewis
    Hi
    Install dateutils and there is ddiff and dadd... check the man page for formatting as required...
    
    Code:
    
    ddiff 2017-11-15 2018-27-08
    350
    I'm trying to avoid having too many dependencies in scanvirus. It now has two. However, I will keep that info noted for something else. Thanks.


    scanvirus is missing the feature: how long since last scan? linux and mswin

    I have various ways of saving that data, including globals and logfile. If I read the logfile backward or grep, I can find the date info. Next question...

    How do I convert a text date into numbers I can use?

    '9-9-18' --> 9-9-18 month=9, day=9, year= 2018

    Parsing using '-'. Some other method?

  6. #6
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    2,418

    Default Re: command to to find difference between dates

    I'm using GNU coreutils "date" as follows in a Bash script to work out when some housekeeping has to be done:
    Code:
    #!/bin/bash
    date +%Y%m%d > ~/.local/share/«Directory and file»
    LastDate=$(cat ~/.local/share/«Directory and file»)
    TwoDaysAgo=$(date --date='2 days ago' +%Y%m%d)
    if [ $LastDate -lt $TwoDaysAgo ]
    then
      «Do something»
    fi
    By adding '%H', '%M', '%S', and %N you can increase the accuracy from "Day" through to "Nanoseconds" if need be …

  7. #7
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    10,823
    Blog Entries
    1

    Default Re: command to to find difference between dates

    Quote Originally Posted by lord_valarian View Post
    Code:
    malcolmlewis
    Hi
    Install dateutils and there is ddiff and dadd... check the man page for formatting as required...
    
    Code:
    
    ddiff 2017-11-15 2018-27-08
    350
    I'm trying to avoid having too many dependencies in scanvirus. It now has two. However, I will keep that info noted for something else. Thanks.


    scanvirus is missing the feature: how long since last scan? linux and mswin

    I have various ways of saving that data, including globals and logfile. If I read the logfile backward or grep, I can find the date info. Next question...

    How do I convert a text date into numbers I can use?

    '9-9-18' --> 9-9-18 month=9, day=9, year= 2018

    Parsing using '-'. Some other method?
    First,
    As jetchisel and dcurtisfra describe,
    date is available by default and supports diffing dates.

    As for changing date formats, I'd recommend you simply avoid the problem instead of doing a conversion (although of course conversion is an option). Create your own application log and create events however you wish, including your preferred data format. Then you can extract data as you wish from your own logs and do as you wish.

    Or,
    Re-think what your task, maybe there is a different and possibly better way of doing what you need to have accomplished.
    For instance, why do you need to diff your dates and then do something with that info? Would simply activating a counter that counts seconds and then execute a task at some threshold accomplish the same objective?

    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  8. #8

    Default Re: command to to find difference between dates

    Quote Originally Posted by lord_valarian View Post
    Code:
    malcolmlewis
    Hi
    Install dateutils and there is ddiff and dadd... check the man page for formatting as required...
    
    Code:
    
    ddiff 2017-11-15 2018-27-08
    350
    I'm trying to avoid having too many dependencies in scanvirus. It now has two. However, I will keep that info noted for something else. Thanks.


    scanvirus is missing the feature: how long since last scan? linux and mswin

    I have various ways of saving that data, including globals and logfile. If I read the logfile backward or grep, I can find the date info. Next question...

    How do I convert a text date into numbers I can use?

    '9-9-18' --> 9-9-18 month=9, day=9, year= 2018

    Parsing using '-'. Some other method?
    Hi,

    Since you insisted in doing your way...
    Will do an example of last year as the date using GNU date(1).

    Code:
    date -d'last year' '+%m-%d-%Y'
    The output should be separated by a dash because we use a literal dash as the separator.

    Now put that in a variable.
    Code:
     var=$(date -d'last year' '+%m-%d-%Y')
    Now we split them into three parts (using the builtin read command from bash) the dash - as the field separator to IFS. and a heredoc <<<

    Code:
    IFS=- read -r month day year <<< "$var"
    Now a verbose and convincing output would be to use declare.

    Code:
    declare -p month day year
    But don't be alarm by the output, since declare just prints that way, help --declare for more info.

    Using printf to see the values

    Code:
    printf '%s\n' "$month" "$day" "$year"
    Another way is to use P.E. parameter expansion.

    Code:
    var=$(date -d'last year' '+%m-%d-%Y')
    year="${var##*-}"
    month="${var%%-*}"
    var="${var%-"$year"*}"
    day="${var##*"$month"-}"
    Do the samething and run both declare -p and printf.

    Code:
    declare -p month day year

    Code:
    printf '%s\n' "$month" "$day" "$year"
    Both solution should work on any POSIX compliant shell (including bash of)
    I gave you what you asked for, but it might be what you need... but hey it is your script...

    One more thing i'd use the format '+%D' but that is using a slash / as the separator. One advantage is that you can convert that into epoch without messing with the delimiter.

    Code:
    var=$(date -d'last year' '+%D')
    Now convert that into Unix epoch time.

    Code:
    date -d"$var" '+%s'
    So now at least you have a time format that you can do math/arithmetic with. Just my 2 cents...
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  9. #9

    Default Re: command to to find difference between dates

    Hi,

    Correction on my previous post. The <<< is a herestring NOT a heredoc <<. which does not work with POSIX sh shells, e.g. the dash shell.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  10. #10

    Default Re: command to to find difference between dates

    Quote Originally Posted by tsu2 View Post
    First,
    As jetchisel and dcurtisfra describe,
    date is available by default and supports diffing dates.

    As for changing date formats, I'd recommend you simply avoid the problem instead of doing a conversion (although of course conversion is an option). Create your own application log and create events however you wish, including your preferred data format. Then you can extract data as you wish from your own logs and do as you wish.

    Or,
    Re-think what your task, maybe there is a different and possibly better way of doing what you need to have accomplished.
    For instance, why do you need to diff your dates and then do something with that info? Would simply activating a counter that counts seconds and then execute a task at some threshold accomplish the same objective?

    TSU

    I'v been reading (and reading again) the various posts (and later again). The current scanvirus log:

    Clean Scan: linux 10-09-2018 10:01:43pm {}
    Clean Scan: mswin 10-10-2018 10:01:43pm {(partitions scanned)}


    These scans I usually run after booting and often more than once a session.

    A counter that functions outside the script would require more complex code. I understand the linux concept of multi-user and multi-processing system. I'm avoiding starting other processes that I would need to later shutdown. Based on all the info presented, I --think-- i'v found the simplest and most practical method. This also avoids adding more dependencies.

    Provide some simple upgrade instructions to the user. Copy older logs somewhere else.

    dateinsecondssince1970 Clean Scan: linux 10-09-2018 10:01:43pm {}

    The first parm is masked out in 'scanvirus -vl'. The script can then make use of new field. Then It should be a simple matter of finding both last mswin and linux scan.

    Also, I make an additional data file holding date of last scans of linux and mswin. Or adding that info to the beginning of the logfile, masking it from 'scanvirus -vl'.


    Thanks to all for the very useful info. I need to work the on the new design more.

Page 1 of 3 123 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •