Reputation: 1383
How can I obtain current date in the following format using Bash
YYYY-MM-DDThh:mm:ss
Upvotes: 25
Views: 22053
Reputation: 705
When you are starting out with the command line, it is so great to have a huge community of users who can answer questions and help you on your way. If you use macOS, some of the very good advice will not work exactly right due to Apple's particular choices and implementations.
There are a lot of ways to do things. I don't have any certain preference either way, except that I just want things to work simply and correctly. Being "compatible" with the majority of users who post on here will make your life simpler. Coreutils
is a free software that will make macOS more "compatible" with standard linux tools.
Nerd Sidebar
If you are interested in the super nerd details and philosophy, or perhaps wish to make your own utilities, I like this page on MaiZure's Projects that explores the design of the tools. This is the official github repo and it contains a variety of tools and improvements from the decades of development that started in 1983. As you might expect, it is a gumbo of different classic sysop languages.
For most people who just want it to work right, install coreutils
and symlink them to replace the originals. Instructions are included in the package.
Coreutils
is a standard (non builtin) set of utilities that is much more "compatible" with much of the "linux-ish" advice you see around the internet. Whichever shell (BASH, ZSH, etc) you use, you can make your life much easier by installing this set of GNU core utilities.
From the GNU website:
Coreutils - GNU core utilities
Introduction to Coreutils
The GNU Core Utilities are the basic file, shell and text manipulation utilities of the GNU operating system. These are the core utilities which are expected to exist on every operating system.
The easiest way to install them on macOS is with Homebrew.
$ brew install coreutils
If this gets you interested, there is also an unrelated active project called moreutils
to consider and produce more useful utils. I definitely consider these more optional and situational, but handy:
$ brew install moreutils
If you are determined to find a specific solution to this problem, add the following to your ~/.bash_profile (or whichever shell you have):
mydate() { [[ "$1" = "-I" ]] && command date "+%Y-%m-%dT%H:%M:%S%z" || command date "$@"; }
There are so many ways to do thing. Most of the assessments of them are necessarily opinionated. There is a rich history of many different tools, simple and effective, that have brought us to where we are today. I love the variety of it. It truly epitomizes the idea of 'your mileage may vary.'
Upvotes: 0
Reputation: 311
Use date -I (iso-8601) format, as already pointed by @karfau
$ man date
-I[FMT], --iso-8601[=FMT]
output date/time in ISO 8601 format. FMT='date' for date only (the default),
'hours', 'minutes', 'seconds', or 'ns' for date and time to the indicated
precision.
Example: 2006-08-14T02:34:56-06:00
Examples:
$ date -I
2018-10-23
$ date -Iminutes
2018-10-23T14:56-07:00
$ date -Iseconds
2018-10-23T14:56:44-07:00
Upvotes: 0
Reputation: 290075
With these options:
$ date "+%Y-%m-%d %H:%M:%S"
2013-10-24 10:40:12
or even (thanks Birei!)
$ date "+%F %T"
2013-10-24 10:40:12
All format controls can be get from man date
.
I don't know what your T
means, but for example you have:
$ date "+%F %T %Z"
2013-10-24 10:40:12 CEST
Upvotes: 42