Giunsa ang Pagtrabaho sa Petsa ug Oras sa Bash Gamit ang date Command


Ang sugo sa petsa usa ka eksternal nga programa sa bash nga nagtugot sa pagtakda o pagpakita sa petsa ug oras sa sistema. Naghatag usab kini daghang mga kapilian sa pag-format. Ang sugo sa petsa gi-install sa tanang Linux distros pinaagi sa default.

$ which date
$ type -a date

I-type ang command date sa terminal nga magpakita karon nga petsa ug oras.

$ date

Ang paggamit sa sugo sa petsa, petsa sa sistema, oras ug timezone mahimong mabag-o ug ang pagbag-o kinahanglan nga i-sync sa orasan sa hardware.

$ date --set="Thu Nov 12 13:06:59 IST 2020"
$ hwclock --systohc

Ang usa ka maayong lugar aron makuha ang lista sa mga kapilian sa pag-format mao ang panid sa tawo.

$ man date

Atong tan-awon ang pipila sa labing kasagaran nga mga kapilian sa pag-format nga among gamiton.

  • Aron magamit ang pag-format gamit ang “+ gisundan sa “formatter“.
  • Aron makakuha og listahan sa mga opsyon sa pag-format para sa GNU\LINUX tan-awa ang linked man page.
  • Aron makakuha og listahan sa mga opsyon sa pag-format para sa BSD tan-awa ang linked man page.

Ang duha ka importante nga bahin sa sugo sa petsa mao ang paggamit sa Format +% ug –date nga kapilian.

Karon atong gamiton ang pipila ka pag-format sa sugo sa petsa. Aron magamit ang pag-format, idugang ang plus sign (+) sundan sa %formatter sama sa gipakita sa mga pananglitan.

Atong tan-awon kung giunsa paggamit ang mga formatter nga may kalabutan sa petsa sa usa ka yano nga script sa shell nga gitawag nga 'date.sh'.

# PRINT YEAR,MONTH,DAY AND DATE...

echo "We are in the year = $(date +%Y)"
echo "We are in the year = $(date +%y)"

# Difference between %Y and %y is %Y will print 4 digits while %y will print the last 2 digits of the year.

echo "We are in the month = $(date +%m)"
echo "We are in the month = $(date +%b)"
echo "We are in the month = $(date +%B)"

# Difference between %B and %b is, %B will print full month name while %b will print abbreviated month name.

echo "Current Day of the month = $(date +%d)"

echo "Current Day of the week = $(date +%A)"
echo "Current Day of the week = $(date +%a)"

# Difference between %A and %a is, %A will print full Weekday name while %a will print abbreviated weekday name.

# Instead of formatting to get the date, we can use %D which will print the date as %m/%d/%y or %F which prints in %Y-%M-%d format.

echo "Date using %D = $(date +%D)"
echo "Date using %F = $(date +%F)"

Atong tan-awon kung giunsa paggamit ang mga formatter nga may kalabotan sa oras sa usa ka yano nga script sa shell nga gitawag nga 'time.sh'.

# PRINT HOURS, MINS, SECONDS, NANO SECONDS

echo Hours = $(date +%H)
echo Minutes = $(date +%M)
echo Seconds = $(date +%S)
echo Nanoseconds = $(date +%N)
echo Epoch Time = $(date +%s)

echo "current time = $(date +%H:%M:%S:%N)"

# can also use %T which displays Time in HH:MM:SS format.

echo "current time in 24 hour format = $(date +%T)"

# can also use %r to display time in 12 hour format.

echo "current time in 12 hour format = $(date +%r)"

Uban sa --date o -d flag input mahimong ipasa ingon nga string ug date command nahibalo sa pagdumala niini nga maalamon.

Atong tan-awon ang pipila ka mga pananglitan aron masabtan kung giunsa kini molihok.

# Print yesterday's date and time.
echo "Yesterday = $(date -d "Yesterday")"

# Print Tomorrow date and time.
echo "tomorrow = $(date -d "tomorrow")"

# Find what is the date and time before 10 days from now.
echo "Before 10 days = $(date -d "tomorrow -10 days")"

# Find last month and next month
echo "Last month = $(date -d "last month" "%B")"
echo "Next month = $(date -d "next month" "%B")"

# Find last year and next year
echo "Last Year = $(date -d "last year" "+%Y")"
echo "Next Year = $(date -d "next year" "+%Y")"

# Forecast the weekday
echo "2 days away from today and it comes on weekdays? = $(date -d "Today +2 days" "+%A")

kuwentaha ang gidaghanon sa mga adlaw tali sa 2 gihatag nga mga petsa.

$ echo $(( ( $(date -d "2020-11-10" "+%s") - $(date -d "2020-11-01" "+%s") ) / 86400))

Pangitaa ang gihatag nga tuig nga leap year o dili.

$ for y in {2000..2020}; do date -d $y-02-29 &>/dev/null && echo $y is leap year; done

Pag-assign sa output sa date command sa usa ka variable.

$ TODAY=$(date +%Y-%m-%d)
OR
$ TODAY1=$(date +%F)
$ echo $TODAY 
$ echo $TODAY1

Paghimo og mga log file nga adunay petsa nga gidugang sa filename.

Ang pagdugang sa petsa ug oras samtang nagmugna og mga log file, backup, o mga text file usa ka komon nga operasyon nga kanunay natong masugatan. Atong kuhaon ang usa ka pananglitan, aron makakuha usa ka backup, naghimo kami usa ka script sa shell.

Kini nga script magkuha ug backup gikan sa 00:00 hangtod 23:59 ug gikatakda nga modagan matag adlaw sa 00:00 sa sunod nga adlaw. Gusto namon nga maghimo mga file sa log nga adunay format sa petsa sa kagahapon.

CUSTOM_FORMAT=$(date --date "Yesterday" "+%d-%y-%H:%M")
LOG_FILE=/var/log/custom_application/application_${CUSTOM_FORMAT}.log
echo "Script started" >>  ${LOG_FILE}
...
CODE BLOCKS
...
echo "Script completed" >> ${LOG_FILE}

Mao kana alang niini nga artikulo. Sa kini nga artikulo, nakita namon kung giunsa ang paggamit sa petsa ug oras sa bash sa Linux. Ipahibalo kanamo ang imong feedback.