ค้นหาเว็บไซต์

วิธีทำงานกับวันที่และเวลาใน Bash โดยใช้คำสั่ง date


คำสั่ง Date เป็นโปรแกรมทุบตีภายนอกที่อนุญาตให้ตั้งค่าหรือแสดงวันที่และเวลาของระบบ นอกจากนี้ยังมีตัวเลือกการจัดรูปแบบหลายแบบ คำสั่ง Date ได้รับการติดตั้งใน Linux distros ทั้งหมดตามค่าเริ่มต้น

which date
type -a date

พิมพ์ คำสั่ง date ใน Terminal ซึ่งจะแสดงวันที่และเวลาปัจจุบัน

date

เปลี่ยนวันที่และเวลาของระบบ Linux

การใช้ คำสั่งวันที่ คุณสามารถแก้ไขวันที่ เวลา และเขตเวลาของระบบได้ และการเปลี่ยนแปลงจะต้องซิงค์กับนาฬิกาของฮาร์ดแวร์

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

ตัวเลือกการจัดรูปแบบ

จุดที่ดีในการดูรายการตัวเลือกการจัดรูปแบบคือหน้าคู่มือ

man date

มาดูตัวเลือกการจัดรูปแบบทั่วไปบางส่วนที่เราจะใช้กัน

  • หากต้องการใช้การจัดรูปแบบ ให้ใช้ “+ ตามด้วย “ตัวจัดรูปแบบ
  • หากต้องการดูรายการตัวเลือกการจัดรูปแบบสำหรับ GNU\LINUX โปรดดูที่หน้าคู่มือที่เชื่อมโยง
  • หากต้องการรับรายการตัวเลือกการจัดรูปแบบสำหรับ BSD โปรดดูที่หน้าคู่มือที่เชื่อมโยง

สองส่วนสำคัญของคำสั่ง date คือการใช้ตัวเลือกรูปแบบ +% และ –date

ตอนนี้ ลองใช้การจัดรูปแบบกับ คำสั่ง date หากต้องการใช้การจัดรูปแบบ ให้เพิ่มเครื่องหมายบวก (+) ตามด้วย %formatter ดังที่แสดงในตัวอย่าง

การจัดการวันที่ใน Linux

มาดูวิธีใช้ตัวจัดรูปแบบที่เกี่ยวข้องกับวันที่ในเชลล์สคริปต์ง่ายๆ ชื่อ '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)"

วันทำการจัดการใน Linux

มาดูวิธีใช้ตัวจัดรูปแบบที่เกี่ยวข้องกับ เวลา ในเชลล์สคริปต์ง่ายๆ ที่เรียกว่า '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)"

ด้วย –date หรือ -d Flag

ด้วย --date หรือ -d อินพุตแฟล็กสามารถส่งผ่านเป็นสตริงได้ และ คำสั่ง date รู้ว่าต้องจัดการอย่างชาญฉลาด

มาดูตัวอย่างเพื่อทำความเข้าใจวิธีการทำงาน

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")

การดำเนินงานทั่วไป

คำนวณจำนวนวันระหว่างวันที่กำหนด 2 วัน

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

ค้นหาปีที่กำหนดว่าเป็นปีอธิกสุรทินหรือไม่

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

การกำหนดเอาต์พุตของคำสั่ง date ให้กับตัวแปร

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

สร้างไฟล์บันทึกโดยเพิ่มวันที่ลงในชื่อไฟล์

การเพิ่มวันที่และเวลาในขณะที่สร้างไฟล์บันทึก ข้อมูลสำรอง หรือไฟล์ข้อความเป็นการดำเนินการทั่วไปที่เราจะพบบ่อยที่สุด มาดูตัวอย่างกัน ในการสำรองข้อมูล เราได้สร้างเชลล์สคริปต์ขึ้นมา

สคริปต์นี้จะทำการสำรองข้อมูลตั้งแต่เวลา 00:00 น. ถึง 23:59 น. และกำหนดให้ทำงานทุกวันเวลา 00:00 น. ของวันถัดไป เราต้องการสร้างไฟล์บันทึกด้วยรูปแบบวันที่ของเมื่อวาน

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}

เพียงเท่านี้สำหรับบทความนี้ ในบทความนี้ เราได้เห็นวิธีใช้ bash date and time ใน Linux แล้ว แจ้งให้เราทราบความคิดเห็นของคุณ