An alias for getting an ISO-8601 compliant datetime string on WSL2

A quick alias for getting an ISO-8601 compliant formatted datetime using WSL 2 and Ubuntu.

The ISO-8601 date format with timestamp, including timezone, should be formatted as YYYY-MM-DDThh:mm:ss[.mmm]TZD.

If you, like me, often need to get the fully-formatted string, this alias will save you some headache:

isonow() {
  inputs="$@"
  option="${inputs%% *}"

  case $option in
    -c) date +%Y-%m-%dT%H:%M:%S%:z | tr -d '\n' | clip.exe;;
    *) date +%Y-%m-%dT%H:%M:%S%:z;;
  esac
}

Add this to your .bash_aliases file or your .bashrc file. Run isonow to print the formatted string, or isonow -c to copy it to directly your clipboard.

Posted: 25.05.2022

Built: 30.11.2022

Updated: 24.05.2022

Hash: d915b33

Words: 95

Estimated Reading Time: 1 minute