40

I am trying to display a date time in the form of

07/08/2013 16:52:13

by using command in bash script:

dt=`date '+%d/%m/%Y_%H:%M:%S'`

which variable is used to populate a CSV file.

The only char accepted is "_" (underscore) or "-" between date and time, output is

07/08/2013_16:52:13

How can I get a space in between date and time? I tried almost (I hope) everything.

  • 5
    ” and “-” are the only characters accepted by _who? What stops you replacing the “_” with “ ” in date's format string? – manatwork Aug 8 '13 at 12:52
  • Nothing prevents the inclusion of the " " in the command, they just don't appear in the output. Try date +"%b %e" and you will get "Sep 4" (one space) for today, not the desired 2. I'm using it in a script and my solution is to do the operation in 3 steps (I actually combine them into one line of code but you get the idea). – Jim2B Sep 4 '15 at 16:18
  • With all due respect, how did this question get 20 votes?  And how did it get into the Reopen Queue? – G-Man Says 'Reinstate Monica' Jan 19 '19 at 2:20
70
#!/bin/bash

dt=$(date '+%d/%m/%Y %H:%M:%S');
echo "$dt"

Guess the problem is in 'echoing' to the csv.

| improve this answer | |
  • 3
    if you need milliseconds, use date '+%F %T.%3N'. if you need microseconds, use date '+%F %T.%6N'. if you need nanoseconds, use date '+%F %T.%N'. %F is for date, it gives YYYY-MM-DD, you can use %D instead to get MM-DD-YYYY, %T is for time, HH:MM:SS. – computingfreak Mar 3 '17 at 1:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.