1

I'm not really sure how to phrase this question, but here goes:

I have a command which returns the date of last connection to an app in the format of 'adDateLastConnected: Thursday, August 23, 2018 at 12:00:07 PM'. I managed to strip out the first part, and only display the date, but I'd like to view this in a more standard date format (like 08-23-2018 12:00:07 PM).

Is this a trivial thing, or is it far more complicated than I suspect? I'm not sure of how deep my ignorance goes :-)

2 Answers 2

3

GNU date is pretty good about interpreting human style date formats:

#!/bin/bash
if [[ $(date --version) != *GNU* ]]
then
  echo "Sorry, this script only works with GNU date." >&2
  exit 1
fi

# Make locale independent
export LC_ALL=C

# Strip the " at " part
x="${1// at / }"

echo "ISO time:  $(date --iso-8601=seconds -d "$x")"
echo "Unix time: $(date -d "$x" +%s)"

Example:

$ ./myscript "Thursday, August 23, 2018 at 12:00:07 PM"
ISO time:  2018-08-23T12:00:07-07:00
Unix time: 1535050807
Sign up to request clarification or add additional context in comments.

Comments

0

Its not difficult. You can format with sed or gawk (awk), any one of theses will do the job for you.

An example with AWK:

awk '
   BEGIN { FS = OFS = "," } 
   { split($3, date, /\//)
     $3 = date[3] "-" date[2] "-" date[1]
     print $0 
   }
' infile

3 Comments

I don't think this is right. Try it with Thursday, August 23, 2018 at 12:00:07 PM as the input. You're trying to split on /, but the input doesn't contain any slashes.
was a exemple snippet, this code snippet can be change for his need.
Just splitting isn't enough, though. You have to translate month names to numbers as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.