mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Format And Print The Current Date And Time as a shell til
This commit is contained in:
28
shell/format-and-print-the-current-date-and-time.md
Normal file
28
shell/format-and-print-the-current-date-and-time.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# Format And Print The Current Date And Time
|
||||
|
||||
If I run the `date` command from the terminal, I get output like the following:
|
||||
|
||||
```bash
|
||||
$ date
|
||||
Fri Jan 22 13:45:44 CST 2021
|
||||
```
|
||||
|
||||
If I want to format the date output different, perhaps for inclusion in a shell
|
||||
script, I can do something like this:
|
||||
|
||||
```bash
|
||||
$ date +"%Y/%m/%d %H:%M:%S"
|
||||
2021/01/22 13:47:55
|
||||
```
|
||||
|
||||
Then I can incorporate that in a script like this:
|
||||
|
||||
```bash
|
||||
now() {
|
||||
echo "Today: $(date +'%Y/%m/%d %H:%M:%S')"
|
||||
}
|
||||
```
|
||||
|
||||
This
|
||||
[page](https://www.tutorialkart.com/bash-shell-scripting/bash-date-format-options-examples/)
|
||||
includes some examples and a page of formatting options.
|
||||
Reference in New Issue
Block a user