Display current date and time without punctuation
This solution should be better.
We can use the date command. Here is an example,
date [OPTION]… [+FORMAT]
We can give the format parameter.It should starting with a +. The meaning of the formatting symbols are here:
- %Y is for year
- %m is for month
- %d is for day
- .. and so on
There are more formatting symbols in man date.
One can get the solution simply by using this code,
#!/bin/bash current_date_time="`date +%Y%m%d%H%M%S`"; echo $current_date_time;
This code is example of shell script.
This is the format of without punctuation:- +%Y%m%d%H%M%S
Here is the syntax for With punctuation :- +%Y-%m-%d %H:%M:%S
To display the current date and time without punctuation is explained here.
The below commands for bash users,
printf '%(%Y%m%d%H%M%S)T' # prints the current time printf '%(%Y%m%d%H%M%S)T' -1 # same as above printf '%(%Y%m%d%H%M%S)T' -2 # prints the time the shell was invoked
To store the result in $varname ,instead of printing it to stdout we could use -v varname ,
printf -v varname ‘%(%Y%m%d%H%M%S)T’
Here the printf is a builtin command and will therefore be faster. While the date command will always be executed in a subshell .That means the process which in unconnect.