Fish Shell:
Parsing a date from a string

How to:

In Fish Shell, you don’t have built-in commands specifically designed for parsing dates from strings. Instead, you rely on external utilities like date (available in Linux and macOS) or leverage popular third-party tools such as GNU date for more complex parsing. Here’s how to approach it:

Using date with Fish:

To parse a date string in the format “YYYY-MM-DD”, you can use the date command with the -d (or --date for GNU date) option followed by the string. The + option is used to format the output.

set date_str "2023-04-01"
date -d $date_str +"%A, %d %B %Y"
# Output: Saturday, 01 April 2023

For macOS (which requires a different format for the -j and -f flags):

set date_str "2023-04-01"
date -j -f "%Y-%m-%d" $date_str +"%A, %d %B %Y"
# Output: Saturday, 01 April 2023

Using GNU date for complex parsing:

GNU date is more flexible with string formats. It can auto-detect many common date string formats without explicitly specifying the input format:

set complex_date_str "April 1, 2023 14:00"
date -d "$complex_date_str" '+%Y-%m-%d %H:%M:%S'
# Output: 2023-04-01 14:00:00

However, when working with date strings that may not be automatically recognized or when precise control over the input format is needed, specifying the input format with GNU date isn’t directly supported. In such cases, consider preprocessing the string or using another tool designed for more complex date parsing routines.