Converting a date into a string

Fish Shell:
Converting a date into a string

How to:

Fish shell keeps things straightforward. Let’s format the current date:

set formatted_date (date "+%Y-%m-%d")
echo $formatted_date

Sample output:

2023-04-11

Want something more specific, like the day of the week?

set day_of_week (date "+%A")
echo $day_of_week

Sample output:

Tuesday

How about we add the time? Here’s the date and time in a 24-hour format:

set date_and_time (date "+%Y-%m-%d %H:%M:%S")
echo $date_and_time

Sample output:

2023-04-11 21:30:47

Deep Dive

In the past, Unix-like systems such as Linux adopted the date command, which has evolved over time and remains prevalent in shells like bash and zsh. Fish shell inherits this but encourages a more readable, flag-less syntax for setting variables.

There are alternatives, such as the strftime function in many programming languages. Fish doesn’t natively support this, but date in UNIX is versatile enough to cover most needs.

When converting a date to a string, the format specifiers, like %Y for the year or %A for the weekday, follow the POSIX standard. The date command uses these specifiers to extract and format specific parts of the date.

It’s important to note that, because dates and times are so locale and timezone dependent, the strings produced can vary unless specified. You can set the timezone before invoking date:

set TZ 'America/New_York'
set date_with_timezone (date "+%Y-%m-%d %H:%M:%S %Z")
echo $date_with_timezone

This ensures you’ve considered the locality of your data—a detail not to skim over in a globalized world.

See Also

  • The man page for date (online manual) gives you the full scoop on format specifiers.
  • For broader context, read about POSIX standards.
  • Check out Fish shell’s official documentation on variables to understand the set command better.