Let me explain you the problem
$ date +%c -d "$d" Tue 31 Dec 2013 01:13:06 PM CET $ date +'Today is %F' -d "$d" Today is 2013-12-31
This solution corresponds to current date.
But I have one variable which stores date other than current date
$Prev_date="Wed Dec 25 06:35:02 EST 2013"
I am looking for solution to read this date as 2013-12-25 and store it in a variable.
I have tried this:
a=`date --date=$Prev_date '+%y/%m/d'` echo $a
It’s giving this error:
date: illegal option -- date=Wed usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff]
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I assume you are talking about Bash. If so, then you are missing the " around the arguments of the --date parameter.
Instead of
a=`date --date=$Prev_date '+%y/%m/d'`
try this
a=`date --date="$Prev_date" '+%y/%m/d'`
and I’m guessing the d is supposed to have a %. So then it would be like that:
a=`date --date="$Prev_date" '+%y/%m/%d'`
The reasons why your error showed you the usage of the date command is following:
Without the " around $Prev_date, the variable will be substituted and the command looks like this:
a=`date --date=Wed Dec 25 06:35:02 EST 2013 '+%y/%m/d'`
So only the Wed is taken as argument to --date, while all the other parts of the $Prev_date string are considered separate parameters to the date command. So date says it doesn’t know a parameter called Dec and shows you it’s help output.
Method 2
The -d option is GNU specific.
Here, you don’t need to do date calculation, just rewrite the string which already contains all the information:
a=$(printf '%sn' "$Prev_date" | awk '{
printf "%04d-%02d-%02dn", $6,
(index("JanFebMarAprMayJunJulAugSepOctNovDec",$2)+2)/3,$3}')
Method 3
If I understand right, you want to save the date, so that you can reuse it later to print the same date in different formats. For this, I propose to save the date in a format that can be easily parsed by the date -d command, and let the date command do the formatting.
As far as I know, the format +%Y%m%d %H:%M:%S is the most platform independent. So let’s save the date in this format:
d=$(date '+%Y%m%d %H:%M:%S')
Then later you can print this date in different formats, for example:
$ date +%c -d "$d" Tue 31 Dec 2013 01:13:06 PM CET $ date +'Today is %A' -d "$d" Today is Tuesday $ date +'Today is %F' -d "$d" Today is 2013-12-31
UPDATE
If you are given a date string like Wed Dec 25 06:35:02 EST 2013, then you can try to parse it with date -d and change its format, for example:
$ date +%F -d 'Wed Dec 25 06:35:02 EST 2013' 2013-12-25
This works with GNU date. If it doesn’t work in your system, you can try the gdate command instead, usually it exists in modern systems.
Method 4
As others have pointed out, the problem is that the date stored in $C above is being evaluated before being passed into your subshell (by the use of “), so the subshell is trying to find a literal command named ‘Tue’ (i.e. the start of your date string).
To fix this, you’ll need to pass your date command back into a GNU version of date using the -d option, which will take an existing date as input.
I’ve illustrated this using your example code above:
C=$(date) G=$(gdate '+%Y%m%d' -d "$C") echo "C = $C" echo "G = $G"
Which outputs
C = Tue Dec 31 10:33:29 EST 2013 G = 20131231
Note: I had to use gdate to get this to work on my system (OS X).
If you don’t have a GNU compatible date command, and can’t install it for whatever reason, the best you could do would be to manipulate the date string directly, using something like this:
C=$(date)
D=$(echo "$C" | awk -F' ' '{ printf "%s-%s-%s", $6, $2, $3 }')
echo "C = $C"
echo "D = $D"
which produces the following output:
C = Tue Dec 31 12:03:27 EST 2013 D = 2013-Dec-31
Method 5
On Mac OS X using the FreeBSD date command you could do the transformation via epoch time:
epoch_time="$(LANG=C TZ='EST' date -j -f "%a %b %d %T %Z %Y" "Wed Dec 25 06:35:02 EST 2013" "+%s")" a="$(date -r $epoch_time '+%F') echo "$a" # 2013-12-25
Method 6
It seems that the first output is G. C already contains the first word of the actual date (Tue), so when you try to execute
`tue …format… `
your shell answers that tue is not a valid command.
$ A=date $ echo $($A) Mar 31 déc 2013 12:21:48 CET $ B=$($A) $ echo $B Mar 31 déc 2013 12:22:12 CET
Try just storing date in your C variable, as I did with A.
And by the way, can’t you find better names for them?
Method 7
Assuming you want to store the exact time, using GNU date, I have found the following options can output in a format that date is happy to parse again:
date -R
date --iso-8601=ns # <-- JavaScript's new Date() does not parse this one
date --iso-8601=seconds
date --rfc-3339=ns
date --rfc-3339=seconds
Note that only the ns options record nanoseconds (although most filesystems only store seconds anyway).
(Out of interest, I also tested them in Node.JS and in Firefox, and only the one indicated failed to parse.)
For example, to read the date of a file into a variable, and then parse and display it:
# Record when bash was compiled into a variable
prev_date="$(date --rfc-3339=ns -r /bin/bash)"
# Parse the variable and display in desired format
echo "Date is: $(date -d "$prev_date" '+%y/%m/%d')"
What I would like to know now: How to do the same with BSD date?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0