To format the date for display or use in a shell script on a Linux or Unix system, you can use the date
command.
The date
command allows you to display the current date and time, or to format the date and time in a specific way.
To display the current date and time in a specific format, you can use the -d
option to specify a format string. The format string consists of formatting codes that tell the date
command how to display the date and time.
For example, to display the current date and time in the format "YYYY-MM-DD HH:MM:SS"
, you can use the following command:
date -d '%Y-%m-%d %H:%M:%S'Source:www.lautturi.com
This will display the current date and time in the format "YYYY-MM-DD HH:MM:SS"
, where YYYY
is the year, MM
is the month, DD
is the day, HH
is the hour, MM
is the minute, and SS
is the second.
You can also use the -u
option to display the date and time in Coordinated Universal Time (UTC). For example, to display the current date and time in UTC in the format "YYYY-MM-DD HH:MM:SS"
, you can use the following command:
date -u -d '%Y-%m-%d %H:%M:%S'
To use the formatted date and time in a shell script, you can assign the output of the date
command to a shell variable. For example:
CURRENT_DATE=$(date -d '%Y-%m-%d %H:%M:%S')
This will assign the current date and time in the format "YYYY-MM-DD HH:MM:SS"
to the CURRENT_DATE
shell variable. You can then use the CURRENT_DATE
variable in your script as needed.
For more information about formatting the date and time using the date
command, you can consult its documentation or seek assistance from a qualified Linux or Unix administrator.