Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

#Bash + coreutils, 50

Bash + coreutils, 50

date -d1ドル+$[(2ドル-`date -d1ドル +%u`+9)%7-3]day +%Y%m%d

Try it online.

  • date -d1ドル +%u gives the day of week (1..7); 1 is Monday
  • This is subtracted from the input day of week to give a difference of day indexes. We want to add a factor between in the range [-3, 3] to the input date to get the closest date, so we add a magic factor 9, take the modulo 7, then subtract 3. The magic factor 9 is 3 + 7 - 1. The 3 adjusts the input range for range [-3, 3], and is subtracted again after the modulo. The 7 is required because bash modulo is the same as CPU modulo and can give -ve results - adding 7 adjusts into the right range. The -1 is because in the input, 1 is Sunday, but with the %u output, 1 is Monday.
  • The outer date then parses <input date> + <magic factor>days and presents it in the YYYYMMDD format.

#Bash + coreutils, 50

date -d1ドル+$[(2ドル-`date -d1ドル +%u`+9)%7-3]day +%Y%m%d

Try it online.

  • date -d1ドル +%u gives the day of week (1..7); 1 is Monday
  • This is subtracted from the input day of week to give a difference of day indexes. We want to add a factor between in the range [-3, 3] to the input date to get the closest date, so we add a magic factor 9, take the modulo 7, then subtract 3. The magic factor 9 is 3 + 7 - 1. The 3 adjusts the input range for range [-3, 3], and is subtracted again after the modulo. The 7 is required because bash modulo is the same as CPU modulo and can give -ve results - adding 7 adjusts into the right range. The -1 is because in the input, 1 is Sunday, but with the %u output, 1 is Monday.
  • The outer date then parses <input date> + <magic factor>days and presents it in the YYYYMMDD format.

Bash + coreutils, 50

date -d1ドル+$[(2ドル-`date -d1ドル +%u`+9)%7-3]day +%Y%m%d

Try it online.

  • date -d1ドル +%u gives the day of week (1..7); 1 is Monday
  • This is subtracted from the input day of week to give a difference of day indexes. We want to add a factor between in the range [-3, 3] to the input date to get the closest date, so we add a magic factor 9, take the modulo 7, then subtract 3. The magic factor 9 is 3 + 7 - 1. The 3 adjusts the input range for range [-3, 3], and is subtracted again after the modulo. The 7 is required because bash modulo is the same as CPU modulo and can give -ve results - adding 7 adjusts into the right range. The -1 is because in the input, 1 is Sunday, but with the %u output, 1 is Monday.
  • The outer date then parses <input date> + <magic factor>days and presents it in the YYYYMMDD format.
added 758 characters in body
Source Link
Digital Trauma
  • 73.7k
  • 10
  • 116
  • 268

#Bash + coreutils, 50

date -d1ドル+$[(2ドル-`date -d1ドル +%u`+9)%7-3]day +%Y%m%d

Try it online.

  • date -d1ドル +%u gives the day of week (1..7); 1 is Monday
  • This is subtracted from the input day of week to give a difference of day indexes. We want to add a factor between in the range [-3, 3] to the input date to get the closest date, so we add a magic factor 9, take the modulo 7, then subtract 3. The magic factor 9 is 3 + 7 - 1. The 3 adjusts the input range for range [-3, 3], and is subtracted again after the modulo. The 7 is required because bash modulo is the same as CPU modulo and can give -ve results - adding 7 adjusts into the right range. The -1 is because in the input, 1 is Sunday, but with the %u output, 1 is Monday.
  • The outer date then parses <input date> + <magic factor>days and presents it in the YYYYMMDD format.

#Bash + coreutils, 50

date -d1ドル+$[(2ドル-`date -d1ドル +%u`+9)%7-3]day +%Y%m%d

Try it online.

#Bash + coreutils, 50

date -d1ドル+$[(2ドル-`date -d1ドル +%u`+9)%7-3]day +%Y%m%d

Try it online.

  • date -d1ドル +%u gives the day of week (1..7); 1 is Monday
  • This is subtracted from the input day of week to give a difference of day indexes. We want to add a factor between in the range [-3, 3] to the input date to get the closest date, so we add a magic factor 9, take the modulo 7, then subtract 3. The magic factor 9 is 3 + 7 - 1. The 3 adjusts the input range for range [-3, 3], and is subtracted again after the modulo. The 7 is required because bash modulo is the same as CPU modulo and can give -ve results - adding 7 adjusts into the right range. The -1 is because in the input, 1 is Sunday, but with the %u output, 1 is Monday.
  • The outer date then parses <input date> + <magic factor>days and presents it in the YYYYMMDD format.
Source Link
Digital Trauma
  • 73.7k
  • 10
  • 116
  • 268

#Bash + coreutils, 50

date -d1ドル+$[(2ドル-`date -d1ドル +%u`+9)%7-3]day +%Y%m%d

Try it online.

AltStyle によって変換されたページ (->オリジナル) /