Programming

Bash에서 날짜 (현재 시간 전날)를 가져옵니다.

procodes 2020. 6. 4. 21:17
반응형

Bash에서 날짜 (현재 시간 전날)를 가져옵니다.


Bash에서 현재 시간 전날의 날짜를 어떻게 인쇄합니까?


GNU 날짜를 가지고 있고 당신을 올바르게 이해했다면

$ date +%Y:%m:%d -d "yesterday"
2009:11:09

또는

$ date +%Y:%m:%d -d "1 day ago"
2009:11:09

BSD (OSX) date가 있다면 다음과 같이 할 수 있습니다 :

date -j -v-1d
Wed Dec 14 15:34:14 CET 2011

또는 임의의 날짜에 날짜 계산을 수행하려는 경우 :

date -j -v-1d -f "%Y-%m-%d" "2011-09-01" "+%Y-%m-%d"
2011-08-31

date --date='-1 day'

맥 OS X

어제 날짜 :

date -v-1d +%F

여기서 1d 는 현재 일에서 1 일을 뺀 값을 정의합니다. 비슷하게,

날짜 -v-1w + % F- 이전 주 날짜

날짜 -v-1m + % F- 이전 달 날짜

GNU 날짜가 있다면

date --date="1 day ago"

자세한 정보 : https://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html


글쎄, 이것은 늦은 답변이지만 이것이 작동하는 것 같습니다!

     YESTERDAY=`TZ=GMT+24 date +%d-%m-%Y`;
     echo $YESTERDAY;

고급 배쉬 스크립팅 안내서

date +%Y:%m:%d -d "yesterday"

날짜 형식에 대한 자세한 내용은 대한 매뉴얼 페이지를 참조하십시오 날짜를

date --date='-1 day'

솔라리스 시스템에 대해 언급하지 않아서 죄송합니다. 따라서 -date 스위치는 Solaris bash에서 사용할 수 없습니다.

나는 시간대에 약간의 트릭으로 이전 날짜를 얻을 수 있다는 것을 알았습니다.

DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE

아마도 펄을 사용 하시겠습니까?

perl -e 'print scalar localtime( time - 86400 ) . "\n";'

또는 nawk 및 (ab) use / usr / bin / adb를 사용하십시오.

nawk 'BEGIN{printf "0t%d=Y\n", srand()-86400}' | adb

우연히 너무 ... 미친!

/usr/bin/truss /usr/bin/date 2>&1 | nawk -F= '/^time\(\)/ {gsub(/ /,"",$2);printf "0t%d=Y\n", $2-86400}' | adb

yesterday=`date -d "-1 day" %F`

YYYY-MM-DD 형식의 어제 날짜를 variable에 넣습니다 $yesterday.


#!/bin/bash
OFFSET=1;
eval `date "+day=%d; month=%m; year=%Y"`
# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=`expr $day - $OFFSET`
if [ $day -le 0 ] ;then
month=`expr $month - 1`
if [ $month -eq 0 ] ;then
year=`expr $year - 1`
month=12
fi
set `cal $month $year`
xday=${$#}
day=`expr $xday + $day`
fi
echo $year-$month-$day

date --date='-1 day'


선택한 날짜 형식이 'YYYYMM'인 경우 정규 표현식으로 간단한 계산을 수행 할 수 있습니다.

echo $(($(date +"%Y%m") - 1)) | sed -e 's/99$/12/'

2020 년 1 월에는 201912를 반환합니다. ;-) 그러나 날짜에 계산 옵션이없고 다른 날짜 해석기 옵션 (예 : perl 사용)이없는 경우 해결 방법 일뿐입니다. ;-)


DST 부분을 관리하는 아래 코드를 사용해보십시오.

if [ $(date +%w) -eq $(date -u +%w) ]; then
  tz=$(( 10#$gmthour - 10#$localhour ))
else
  tz=$(( 24 - 10#$gmthour + 10#$localhour ))
fi
echo $tz
myTime=`TZ=GMT+$tz date +'%Y%m%d'`

코트 지 안스가 비에 커스


DST 인식 솔루션 :

Manipulating the Timezone is possible for changing the clock some hours. Due to the daylight saving time, 24 hours ago can be today or the day before yesterday.

You are sure that yesterday is 20 or 30 hours ago. Which one? Well, the most recent one that is not today.

echo -e "$(TZ=GMT+30 date +%Y-%m-%d)\n$(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1

The -e parameter used in the echo command is needed with bash, but will not work with ksh. In ksh you can use the same command without the -e flag.

When your script will be used in different environments, you can start the script with #!/bin/ksh or #!/bin/bash. You could also replace the \n by a newline:

echo "$(TZ=GMT+30 date +%Y-%m-%d)
$(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1

Not very sexy but might do the job:

perl -e 'my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - 86400);$year += 1900; $mon+= 1; printf ("YESTERDAY: %04d%02d%02d \n", $year, $mon, $mday)'

Formated from "martin clayton" answer.


date +%Y:%m:%d|awk -vFS=":" -vOFS=":" '{$3=$3-1;print}'
2009:11:9

참고URL : https://stackoverflow.com/questions/1706882/get-the-date-a-day-before-current-time-in-bash

반응형