Display current date and time without punctuation

Display current date and time without punctuation

Asked on December 21, 2018 in Bash.
Add Comment


  • 3 Answer(s)

    This solution should be better.

    date +%Y%m%d%H%M%S
    
    

    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.

    Answered on December 21, 2018.
    Add Comment

    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

    Answered on December 21, 2018.
    Add Comment

    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.

    Answered on December 21, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.