Date Concepts:
Here’s a complete categorized breakdown of the date
command in Linux for FAANG-level preparation, covering 5 interview-style questions & answers for each category:
📚 1. Displaying Date & Time
Focus: Format strings like
%Y
,%m
,%d
,%H
,%M
,%S
,%A
,%B
, etc.
✅ Sample Questions:
Q1: How do you display the full date in YYYY-MM-DD
format?
A1:
date '+%F'
# Output: 2025-06-07
Q2: Show current time in HH:MM:SS
24-hour format.
A2:
date '+%T'
# Output: 13:40:00
Q3: Print abbreviated weekday and month name.
A3:
date '+%a %b'
# Output: Sat Jun
Q4: How to show the current time with AM/PM format?
A4:
date '+%r'
# Output: 01:40:00 PM
Q5: Print the current local date and time with timezone.
A5:
date '+%c'
# Output: Sat 07 Jun 2025 13:40:00 IST
🧮 2. Date Arithmetic & Relative Time
Focus: Using
--date
to calculate past/future dates or times.
✅ Sample Questions:
Q1: Get the date 7 days ago.
A1:
date --date='7 days ago'
Q2: What will be the date 3 weeks from now?
A2:
date --date='3 weeks'
Q3: What was the date 2 months ago from today?
A3:
date --date='2 months ago'
Q4: Get the time 30 minutes in the future.
A4:
date --date='30 minutes'
Q5: Display the date and time from a specific epoch timestamp (e.g., @1688793600
).
A5:
date -d @1688793600
🕒 3. Timezones & UTC Handling
Focus:
-u
,TZ
, and UTC date/time conversion.
✅ Sample Questions:
Q1: How to display the current time in UTC?
A1:
date -u
Q2: How to display time in a specific timezone, e.g., UTC or PST?
A2:
TZ="America/Los_Angeles" date
Q3: How do you set the date
output format to include timezone?
A3:
date '+%Z %z'
# Output: IST +0530
Q4: Convert local time to UTC using date
.
A4:
date -u '+%Y-%m-%d %H:%M:%S'
Q5: Set a script to work with UTC time regardless of system local time.
A5:
TZ=UTC date
🛠4. Formatting for File/Log Naming
Focus: Timestamps for logs, backup scripts, file naming.
✅ Sample Questions:
Q1: Generate a log file with a unique timestamp suffix.
A1:
filename="log_$(date '+%Y%m%d_%H%M%S').log"
Q2: Use date output for folder naming.
A2:
mkdir "$(date '+%Y-%m-%d')"
Q3: What’s the format string for YYYYMMDD-HHMM
?
A3:
date '+%Y%m%d-%H%M'
Q4: How to generate filenames like backup-20250607.tar.gz
?
A4:
tar -czf "backup-$(date '+%Y%m%d').tar.gz" folder/
Q5: Add date/time to syslog manually in a script.
A5:
echo "$(date '+%F %T') - Service restarted" >> syslog.txt
📆 5. Parsing & Comparing Dates
Focus: Converting and comparing two dates using
date
and epoch.
✅ Sample Questions:
Q1: How to convert 07/06/2025
to 2025-06-07
?
A1:
date -d '07/06/2025' '+%Y-%m-%d'
Q2: Get Unix timestamp of a given date.
A2:
date -d '2025-06-07 12:00:00' +%s
Q3: Calculate days between two dates.
A3:
start="2025-06-01"
end="2025-06-07"
echo $(( ($(date -d "$end" +%s) - $(date -d "$start" +%s)) / 86400 ))
Q4: How do you compare two timestamps in a Bash script?
A4:
if [ "$(date -d '2025-06-07' +%s)" -gt "$(date -d '2025-06-01' +%s)" ]; then
echo "Later date"
fi
Q5: Convert a given MM/DD/YYYY
string to timestamp.
A5:
date -d '06/07/2025' +%s
Here is a comprehensive list of 50 Linux date
command examples, including FAANG-level Q&A format with explanations to help you master time/date handling in Linux environments.
📅 50 Linux date
Command Examples & Interview Questions
🧩 Basic Date Commands
Q1. How to display the current date and time?
date
Q2. How to display the date in YYYY-MM-DD
format?
date +"%F"
🔹 Output: 2025-06-07
Q3. How to display only the current time?
date +"%T"
🔹 Output: 14:30:55
Q4. Show full weekday, month, date, and year
date +"%A, %B %d, %Y"
🔹 Output: Saturday, June 07, 2025
Q5. Show date in MM/DD/YYYY
format
date +"%m/%d/%Y"
Q6. Show Unix timestamp (seconds since epoch)
date +%s
Q7. Convert a Unix timestamp to human-readable format
date -d @1684511800
Q8. Display current time in 12-hour format with AM/PM
date +"%r"
Q9. How to display the current time zone?
date +"%Z %z"
Q10. Display abbreviated weekday and month
date +"%a, %b"
🧠Manipulating Dates
Q11. What is the date 7 days from now?
date -d "+7 days"
Q12. Show yesterday's date
date -d "yesterday"
Q13. Show tomorrow’s date
date -d "tomorrow"
Q14. Show the date 3 months ago
date -d "3 months ago"
Q15. What will be the date 1 year from now?
date -d "next year"
Q16. Show the last Sunday’s date
date -d "last sunday"
Q17. Show the first day of next month
date -d "$(date +%Y-%m-01) +1 month"
Q18. Add 2 hours to current time
date -d "+2 hours"
Q19. Subtract 45 minutes
date -d "45 minutes ago"
Q20. Show date at 00:00 today
date -d "today 00:00"
🛠️ Setting Date and Time
Q21. Set date to 1 July 2018, 3:15 AM
sudo date -s "2018-07-01 03:15:00"
Q22. Set only the time
sudo date -s "03:15:00"
Q23. Set date using MMDDhhmmYYYY
format
sudo date 070103152018
📌 Format: Month-Day-Hour-Min-Year
Q24. How to set time using ISO format
sudo date -s "2025-06-07T14:00:00"
Q25. Set hardware clock from system clock
sudo hwclock --systohc
Q26. Set system clock from hardware clock
sudo hwclock --hctosys
Q27. Display hardware clock time
sudo hwclock --show
Q28. Set date via ntpdate
from internet
sudo ntpdate pool.ntp.org
Q29. Set timezone via timedatectl (systemd)
sudo timedatectl set-timezone Asia/Kolkata
Q30. Show current systemd time settings
timedatectl
🧪 Formatting and Conversion
Q31. Format date as: 07-Jun-2025
date +"%d-%b-%Y"
Q32. Print week number
date +"%U"
Q33. Print day of the year (001–366)
date +"%j"
Q34. Print ISO 8601 date
date -I
Q35. Convert string to epoch
date -d "2025-06-07 12:00:00" +%s
Q36. Convert epoch to date string
date -d @1686120000
Q37. Get full date and time with nanoseconds
date +"%F %T.%N"
Q38. Show UTC time
date -u
Q39. Compare time difference between two timestamps (manually)
echo $(( $(date -d "now" +%s) - $(date -d "1 hour ago" +%s) ))
Q40. List all available timezones
timedatectl list-timezones
🧱 Scripting & Advanced
Q41. Get current date as variable in shell script
today=$(date +%F)
echo "Today is $today"
Q42. Display log files created in the last 24 hours (based on date
)
find /var/log -type f -newermt "$(date -d '1 day ago')"
Q43. Log script runtime using date
start=$(date +%s)
# your script
end=$(date +%s)
echo "Runtime: $((end - start)) seconds"
Q44. Create folder with today's date
mkdir "$(date +%Y-%m-%d)"
Q45. Create a filename with timestamp
touch "logfile_$(date +%Y%m%d_%H%M%S).log"
Q46. Check if system time is synced (systemd)
timedatectl | grep "System clock synchronized"
Q47. Print date of the next Monday
date -d "next monday"
Q48. Print start of the current month
date +"%Y-%m-01"
Q49. Get yesterday in YYYYMMDD
format
date -d "yesterday" +"%Y%m%d"
Q50. How to simulate future time for testing scripts
date -d "next week" +"%Y-%m-%d %H:%M:%S"
https://delightlylinux.wordpress.com/2024/06/11/bash-get-the-date-and-time/