How to change this time output ?
date --date="#$(echo $(TZ=UTC date +%s) - $(date +%s)'%(5*60)-(5*60)' | bc)"
Output: za apr 12 00:25:00 CEST 2014
Should output in this layout: %Y%m%d%H%M
How to implement this in the string ?
Thanks!!
i think this should do what you want:
d="#$(echo $(TZ=UTC date +%s) - $(date +%s)'%(5*60)-(5*60)' | bc)"&&echo `date --date="$d"` `date --date="$d" +%Y%m%d%H%M`
d="#$(echo $(TZ=UTC date +%s) - $(date +%s)'%(5*60)-(5*60)' | bc)"&&echo `date --date="$d" --utc` `date --date="$d" +%Y%m%d%H%M --utc`
Second one is in UTC.
Related
I was hoping to get some help with finding the difference in days between two dates.
Date 1: Sysdate
Date2: Mar 20 2022 (future)
What I was trying to do is convert it to EPOCH time, subtract and then divide by 86400 to get the number of days between the two dates. However, I've having issues with the systax. Here's what I've tried:
days_remaining=('date "+%s" -d "Mar 20 2022"'-'date "+%s" -d "$(date '+%b %d %Y')"')/86400
But the above isn't working. Any assistance would be appreciated.
To do calculations in bash you need an arithmetic context (( )). Also, to execute your date commands you have to put them inside $() instead of string quotes ''.
By the way: The last date command in date "+%s" -d "$(date '+%b %d %Y') isn't necessary. date -d 0:00 +%s will print the same unix time.
(( days_remaining = ($(date -d "Mar 20 2022" +%s) - $(date -d 0:00 +%s)) / 86400 ))
you can use this
days_remaining=$(($(($(date "+%s" -d "Mar 20 2022") - $(date "+%s" -d "$(date '+%b %d %Y')"))) / 86400))
For example:
echo $(date) - $(date -r sample.txt)
Output:
90 days(for example)
Use %s seconds since 1970-01-01 00:00:00 UTC as in
echo $(expr $(date +%s) - $(date -r sample.txt +%s)) #!/bin/sh
echo $(($(date +%s) - $(date -r sample.txt +%s))) #/bin/bash
One more way
$ ls -l peter.txt
-rwxrw-r--+ 1 pppp qqqq 149 Dec 15 18:39 peter.txt
$ echo "(" $(date +%s) - $(date -r peter.txt +%s) ")/" 86400 | perl -nle ' print eval, " days" '
29.254537037037 days
$
How can I convert a date variable to seconds?
I have a variable called CDATE defined like this:
CDATE=$(/bin/date +"%Y-%m-%d %H:%M:%S")
CDATE example output: "2012-12-12 12:12:12"
I would like to convert this to time in seconds and save it in another variable.
x=($(date +"%Y-%m-%d %H:%M:%S %s"))
CDATE="${x[0]} ${x[1]}"
secs="${x[2]}" # seconds since 1970-01-01 00:00:00 UTC
echo $CDATE
echo $secs
Output:
2015-04-04 14:13:08
1428149588
On Linux:
CDATE=$(date "+%Y-%m-%d %H:%M:%S")
SECONDS=$(date -d "$CDATE" +%s)
On Mac:
CDATE=$(date "+%Y-%m-%d %H:%M:%S")
SECONDS=$(date -j -f "%Y-%m-%d %H:%M:%S" "$CDATE" +%s)
And the content of the two variables is (in both cases):
$ echo "$CDATE"
2015-04-04 16:24:41
$ echo "$SECONDS"
1428157497
If you are not charged for CPU time, then I would call date twice to make it more readable:
# get the timestamp:
timestamp=$( date +"%s" )
# format it:
CDATE=$( date -d #"$timestamp" +"%Y-%m-%d %H:%M:%S" )
echo "$CDATE"
echo "$timestamp"
The simplest way:
source <(date +"CDATE='%Y-%m-%d %H:%M:%S' EPOCH='%s'")
Example:
tiago#dell:/tmp$ source <(date +"CDATE='%Y-%m-%d %H:%M:%S' EPOCH='%s'")
tiago#dell:/tmp$ echo "cdate:$CDATE epoch:$EPOCH"
cdate:2015-04-05 12:39:58 epoch:1428233998
In short, I want something works like this:
When I input a date like 2012-12-27 and want to expand the date by
week(start with Monday), it
outputs:2012-12-24,2012-12-25,2012-12-26,2012-12-27,2012-12-28,2012-12-29,2012-12-30
When I input a date like 2012-12-27 and want to expand the date by month, it outputs:2012-12-01,2012-12-02 ... 2012-12-31
or, how can I group a bunch of dates by week? e.g. when I input2012-12-01,2012-12-02 ... 2012-12-31. It outputs:2012-12-01,2012-12-02|2012-12-03 ... 2012-12-09|2012-12-10 ... 2012-12-16|...|2012-12-31
I have no idea how to complete this, any clue may be helpful!
DAYSECS=86400 # seconds in a day
WEEKSECS=604800
echo "Expand on week:"
g_epoch=$(date +"%s" -d $1) # given date as seconds from epoch
g_dayno=$(date +"%u" -d $1) # given date as day of week
g_month=$(date +"%m" -d $1) # given month
g_year=$(date +"%Y" -d $1) # given year
s_epoch=$(($g_epoch - $DAYSECS * ($g_dayno - 1)))
e_epoch=$(($s_epoch + $WEEKSECS))
for etime in $(seq $s_epoch $DAYSECS $e_epoch); do
date +"%Y-%m-%d" -d "#$etime"
done
echo "Expand on month:"
s_epoch=$(date +"%s" -d "$g_year-$g_month-01")
e_epoch=$(($s_epoch + 4 * $WEEKSECS))
for etime in $(seq $s_epoch $DAYSECS $e_epoch); do
if [ $(date +"%m" -d "#$etime") -ne "$g_month" ]; then
break;
fi
date +"%Y-%m-%d" -d "#$etime"
done
The script below work out from #perreal, I leave it here because:
It shows the power of GNU date.
It makes #perreal 's idea more clear and more universal.
Thank you, perreal!
Here it is
dd="2012-12-27" # test date
DAYSECS=86400 # seconds in a day
echo "expand by week:"
s_epoch=$(date +%s -d "$dd -$(($(date +%u -d $dd) - 1)) day") # start date of the week
e_epoch=$(date +%s -d "1970-01-01 00:00:00 +0000 +${s_epoch} seconds +6 days") # end date of the week
for etime in $(seq $s_epoch $DAYSECS $e_epoch); do
date +"%Y-%m-%d" -d "#$etime";
done
echo "expand by month:"
s_epoch=$(date +%s -d "$dd -$(($(date +%d -d $dd) - 1)) day") # start date of the month
e_epoch=$(date +%s -d "1970-01-01 00:00:00 +0000 +${s_epoch} seconds +1 month -1 day") # end date of the month
for etime in $(seq $s_epoch $DAYSECS $e_epoch); do
date +"%Y-%m-%d" -d "#$etime";
done
If there are dates as 2010-06-01 and another as 2010-05-15
Using shell script or date command how to get the number of days between the two dates
Thanks..
Using only date and shell arithmetics:
echo $((($(date -d "2010-06-01" "+%s") - $(date -d "2010-05-15" "+%s")) / 86400))
There's a solution that almost works: use the %s date format of GNU date, which prints the number of seconds since 1970-01-01 00:00. These can be subtracted to find the time difference between two dates.
echo $(( ($(date -d 2010-06-01 +%s) - $(date -d 2010-05-15 +%s)) / 86400))
But the following displays 0 in some locations:
echo $((($(date -d 2010-03-29 +%s) - $(date -d 2010-03-28 +%s)) / 86400))
Because of daylight savings time, there are only 23 hours between those times. You need to add at least one hour (and at most 23) to be safe.
echo $((($(date -d 2010-03-29 +%s) - $(date -d 2010-03-28 +%s) + 43200) / 86400))
Or you can tell date to work in a timezone without DST.
echo $((($(date -u -d 2010-03-29 +%s) - $(date -u -d 2010-03-28 +%s)) / 86400))
(POSIX says to call the reference timezone is UTC, but it also says not to count leap seconds, so the number of seconds in a day is always exactly 86400 in a GMT+xx timezone.)
OSX date is different than GNU date. Got it working like this in OSX. This is not portable solution.
start_date=$(date -j -f "%Y-%m-%d" "2010-05-15" "+%s")
end_date=$(date -j -f "%Y-%m-%d" "2010-06-01" "+%s")
echo $(( ($end_date - $start_date) / (60 * 60 * 24) ))
Idea is still same as in the other answers. Convert dates to epoch time, subtract and convert result to days.
Got it
d1=`date +%s -d $1`
d2=`date +%s -d $2`
((diff_sec=d2-d1))
echo - | awk -v SECS=$diff_sec '{printf "Number of days : %d",SECS/(60*60*24)}'
thanks..
Gnu date knows %j to display the day in year:
echo $(($(date -d 2010-06-01 +%j) - $(date -d 2010-05-15 +%j)))
crossing year-boundaries will give wrong results, but since you gave fixed dates ...