<< Google Suggest | Home | Spring 1.1.3 Comes To JPackage.org >>

How Many Days Ago Was That?

After doing my "how many days ago" calculation in my head once too many, I set out to find a way to let my computer do the work. After trying Java and C, I settled on this bash script:

[weiqi@gao] $ cat ~/bin/daysago
#!/bin/bash
#
# Usage: daysago [date]
#
bc <<<`date +%j`-`date -d $1 +%j`

[weiqi@gao] $ daysago 11/23
18

Can you name all the bash features used in this script?



Re: How Many Days Ago Was That?

Weiqi, I posted a Java solution.

Re: How Many Days Ago Was That?

I think you'll be disappointed on January 1.

This works with years:

bc <<<"(`date +%s`-`date -d $1 +%s`)/60/60/24"

Re: How Many Days Ago Was That?

For dates before 1/1/1970, an extra pair of parentheses is needed:

bc <<<"(`date +%s`-(`date -d $1 +%s`))/86400"

Re: How Many Days Ago Was That?

Excellent! BTW, date(1) 2.0.12 gives me this:

$ date -d '1969-12-13' +%D
date: invalid date `1969-12-13'
$ date -d '40 years ago' +%D
date: invalid date `40 years ago'

But date(1) 2.0.15 on Cygwin works.

Re: How Many Days Ago Was That?

today date is 23/06/05. i want get 25 days back date. how to get in java

Add a comment Send a TrackBack