Introduction
strftime (string format time) is a function used in many programming languages such as C, Python, and Bash utilities to format date and time into a readable string.
It converts a time value into a formatted string using format specifiers.
You can read more about it using:
man strftime
Many Linux utilities implement this formatting system. One of the most common is the date command, which uses the same strftime format specifiers.
Using the date Command with Custom Format
The date + syntax in Linux means:
Format the date output using a format string
Syntax
date +FORMAT
Everything after the + is interpreted as a strftime format specification.
Example: Custom Date Format
[root@oel01db ~]# date "+%d-%m-%Y"
06-03-2026
[root@oel01db ~]#
Example: Date and Time
[root@oel01db ~]# date "+%d-%m-%Y %H:%M:%S"
06-03-2026 03:01:45
[root@oel01db ~]#
Default date Output
If no format is provided, the system prints the default format.
[root@oel01db ~]# date
Fri Mar 6 03:01:55 IST 2026
[root@oel01db ~]#
Storing Date Format in a Variable
In shell scripting, it is often useful to store the format string in a variable.
This allows you to reuse the same format across multiple commands.
[root@oel01db ~]# datefmt="%d-%m-%Y %H:%M:%S"
[root@oel01db ~]#
[root@oel01db ~]# date +"$datefmt"
06-03-2026 03:09:30
[root@oel01db ~]#
This approach improves script readability and maintainability.
Common strftime Format Specifiers
| Format | Meaning | Example |
|---|---|---|
%Y | year | 2026 |
%y | 2-digit year | 26 |
%m | month | 03 |
%d | day | 04 |
%H | hour (24h) | 05 |
%M | minute | 28 |
%S | seconds | 46 |
%A | weekday | Tuesday |
%B | month name | March |
Printing Time Using Bash printf
Bash provides a built-in way to format time using printf.
Syntax
printf "%(FORMAT)T"
Example:
printf "%(%d-%m-%Y %H:%M:%S)T\n" -1
Why -1 is Used
In Bash, -1 represents the current time.
Example:
[root@oel01db ~]# printf "%(%d-%m-%Y %H:%M:%S)T\n" -1
06-03-2026 03:12:19
[root@oel01db ~]#
[root@oel01db ~]# printf "%(%d-%m-%Y %H:%M:%S)T\n" -1
06-03-2026 03:12:22
[root@oel01db ~]#
Each execution prints the current system time.
[root@oel01db ~]# printf -v START_TIME "%(%Y-%m-%d %H:%M:%S)T" -1
[root@oel01db ~]#
[root@oel01db ~]# echo "Process started at $START_TIME"
Process started at 2026-03-06 03:21:18
[root@oel01db ~]#
The -d option in the date command means:
Display the date/time described by the given string instead of the current time.
Example: Yesterday's Date
[root@oel01db ~]# date -d "yesterday"
Thu Mar 5 03:25:10 IST 2026
[root@oel01db ~]#
Example: Tomorrow
[root@oel01db ~]# date -d "tomorrow"
Sat Mar 7 03:25:15 IST 2026
[root@oel01db ~]#
Example: Custom Output Format
[root@oel01db ~]# date -d "yesterday" "+%d-%m-%Y"
05-03-2026
[root@oel01db ~]#
Example: Add Days
[root@oel01db ~]# date -d "+5 days"
Wed Mar 11 03:26:10 IST 2026
[root@oel01db ~]#
Example: Subtract Days
[root@oel01db ~]# date -d "-7 days"
Fri Feb 27 03:26:30 IST 2026
[root@oel01db ~]#
Example: Next Month
[root@oel01db ~]# date -d "next month"
Mon Apr 6 03:27:05 IST 2026
[root@oel01db ~]#
Very Useful in Shell Scripts
Get Yesterday in a Specific Format
yesterday=$(date -d "yesterday" "+%Y%m%d")
Common date -d Expressions
| Expression | Meaning |
|---|---|
"yesterday" | previous day |
"tomorrow" | next day |
"+5 days" | 5 days in future |
"-2 weeks" | 2 weeks ago |
"next month" | same day next month |
"last year" | same date last year |
No comments:
Post a Comment