Bash Date FormattingIn this topic, we will learn about available format options for date command and understand how can we use them with Bash Scripts. Bash DateBash shell provides different date commands along with different formatting options. We can use those commands to format Bash date to a required one. Bash Date CommandWe can use the `date` command to display or change the current date and time value of the system. We can print date and time value in different formats by using date command. We can also use this command for calculating date and time value-related tasks. If `date` command is used without any option, then it will print the current system's date and time value. This command contains several formatting options to format the output. The syntax of date command is given below: Format Bash Date with OptionsAs we have discussed above, we can format the Bash Date. We can also use spaces with the format that you are going to use. Date command accepts the options if provided like: If we want to format the date with spaces, we can use the syntax: List of Bash Date Formatting-optionsThere are different types of formatting codes or characters available, which can be used with the date options to generate the formatted output. Following is a list of some common options and format codes for date command: Options:
Format Option Codes
We can use any of the formats as mentioned above (first column) for the date command as per requirement. ExamplesBash Date Format MM-DD-YYYY To use the date in MM-DD-YYYY format, we can use the command date +%m-%d-%Y. Bash Script Program Bash Console View Output It is very important to note that format option codes are case sensitive. In this example, we have used %m for a month, %d for a day and %Y for a year. If we had used %M in place of %m, then it would define minutes. Bash Date Format MM-YYYY To use the date in MM-YYYY format, we can use the command date +%m-%Y. Bash Script Program Bash Console View Output Bash Date Format Weekday DD-Month-YYYY To use the date in Weekday DD-Month, YYYY format, we can use the command date +%A %d-%B, %Y. Bash Script Program Bash Console View Output ConclusionIn this topic, we discussed available date format options and some example demonstrating the usage of them. Next TopicBash Sleep |