How to convert date from number format to String format?

0

My input is [10/04/16 01:02:03 BST].How can I convert to Apr 10 16 01:02:03? And I dont want to replace / to-.

Vishwaroopa

Posted 2016-06-29T06:29:52.907

Reputation: 1

1

You can freely edit your own posts but for your protection, this must be done under the original user account. It looks like you have created a second account, which will also interfere with your ability to comment within your thread and to accept an answer. See Merge my accounts to get your accounts merged, which will solve the problem.

– fixer1234 – 2016-06-29T07:51:19.190

@fixer1234 I don't think there's any reason not to allow this edit. Otherwise the question will stay unclear for quite some time, no? – slhck – 2016-06-29T08:03:18.027

1@slhck, I was having trouble following the edit, and since I couldn't verify the author, figured the safe thing would be to let the OP do it after merging accounts (or that other reviewers could either bless it or clean it up). – fixer1234 – 2016-06-29T08:10:01.027

@Vishwaroopa, it looks like the edit was rejected. If you try it again under the original account, you should be able to do it without review. If you can't remember how to access it and don't want to wait for your accounts to be merged, try the edit again under whatever account you're using. Hopefully, the comments here will lead to the edit being accepted. – fixer1234 – 2016-06-29T08:20:08.083

you may "accept" the answers that solves your concern/issues by clicking on the 'tick' at the answer post.. This will provide fair credit to the author. (and motivate others to ans better too..) ( : – p._phidot_ – 2018-10-15T17:38:28.933

Answers

1

Using awk:

$ echo '[10/04/16 01:02:03 BST]' | awk -F'[][/: ]' 'BEGIN{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",m,/ /)} {print m[$3+0],$2,$4,$5":"$6":"$7}'
Apr 10 16 01:02:03

How it works

  • -F'[][/: ]'

    This sets the field separator to any of [, ], /, :, or blank.

  • BEGIN{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",m,/ /)}

    This defines an array m that contains the month names

  • print m[$3+0],$2,$4,$5":"$6":"$7

    This prints out the date in the new format.

Alternate input with backslashes

If there are backslashes in the input, we can ignore them by adding the backslash to our list of field separators:

$ echo '\[10\/04\/16 01:02:03 BST\]' | awk -F'[][/: \\\\]+' 'BEGIN{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",m,/ /)} {print m[$3+0],$2,$4,$5":"$6":"$7}'
Apr 10 16 01:02:03

John1024

Posted 2016-06-29T06:29:52.907

Reputation: 13 893

1

Depending on your version of date (GNU-based systems), this will do the trick.

date -jf '[%d/%m/%y %T BST]' '[10/04/16 01:02:03 BST]' +'%b %d %g %T'

Invalid dates or times, such as invalid day or invalid month, cause an error. Therefore this won't work.

date -jf '[%d/%m/%y %T BST]' '[10/13/16 01:02:03 BST]' +'%b %d %g %T'

Days beyond the end of a month spill into the following month, so this "wraps" to Mar 02 16 01:02:03.

date -jf '[%d/%m/%y %T BST]' '[31/02/16 01:02:03 BST]' +'%b %d %g %T'

creidhne

Posted 2016-06-29T06:29:52.907

Reputation: 1 262