在 unix 中格式化日期以包含当天的后缀(st、nd、rd 和 th)

发布于 2024-08-26 07:27:42 字数 478 浏览 10 评论 0原文

如何在 unix 日期的数字上添加后缀?

我会解释一下。我有一个 TextMate 捆绑片段,可以写出今天的日期。它使用 unix 日期和格式。代码如下:

`date +%A` `date +%d` `date +%B` `date +%Y`

它输出:

Monday 22 March 2010

我想将后缀添加到 day (st, nd rdth),如下所示:

2010 年 3 月 22 日星期一

据我所知, 中没有本机函数unix 日期格式,就像 PHP (j) 中的格式一样。我将如何在unix中实现这一点?日期数字的复杂正则表达式?

How can I add the suffix on the day number of a unix date?

I'll explain. I have a TextMate bundle snippit that writes out today's date. It uses unix date and formatting. Here is the code:

`date +%A` `date +%d` `date +%B` `date +%Y`

It outputs:

Monday 22 March 2010

I would like to add the suffix to the day (st, nd, rd and th) like so:

Monday 22nd March 2010

As far as I can see, there is no native function in the unix date formatting, like there is in PHP (j). How would I achieve this in unix? A complicated regex on the day number?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你另情深 2024-09-02 07:27:43

尝试。

#!/bin/sh
DaySuffix() {
  case `date +%d` in
    1|21|31) echo "st";;
    2|22)    echo "nd";;
    3|23)    echo "rd";;
    *)       echo "th";;
  esac
}
date "+%A %d`DaySuffix` %B %Y"

Try.

#!/bin/sh
DaySuffix() {
  case `date +%d` in
    1|21|31) echo "st";;
    2|22)    echo "nd";;
    3|23)    echo "rd";;
    *)       echo "th";;
  esac
}
date "+%A %d`DaySuffix` %B %Y"
沒落の蓅哖 2024-09-02 07:27:43

我在 Linux 机器(Ubuntu 8.10)上有类似的工作。我认为它不适用于 Solaris,我测试的一台机器不允许在 % 后面使用 _ 字符以避免用 0 填充该字段。非填充允许日期返回 1 而不是 01(01st 不与第 1 点相比看起来不太正确)。

我使用名为 DaySuffix 的 shell 函数(同样,您的操作系统或 shell 版本可能不喜欢我定义该函数的方式),然后将该 func 作为日期调用的一部分进行调用。 func 本身相当 hacky,我确信有更好的方法来做到这一点,但它对我有用。请注意 11、12 和 11 的特殊情况。 13 - 你必须热爱英语!

#!/bin/sh

DaySuffix() {
    if [ "x`date +%-d | cut -c2`x" = "xx" ]
    then
        DayNum=`date +%-d`
    else
        DayNum=`date +%-d | cut -c2`
    fi

    CheckSpecialCase=`date +%-d`
    case $DayNum in
    0 )
      echo "th" ;;
    1 )
      if [ "$CheckSpecialCase" = "11" ]
      then
        echo "th"
      else
        echo "st"
      fi ;;
    2 )
      if [ "$CheckSpecialCase" = "12" ]
      then
        echo "th"
      else
        echo "nd"
      fi ;;
    3 )
      if [ "$CheckSpecialCase" = "13" ]
      then
        echo "th"
      else
        echo "rd"
      fi ;;
    [4-9] )
      echo "th" ;;
    * )
      return 1 ;;
    esac
}

# Using consolidated date command from chris_l
# Also using %-d instead of %d so it doesn't pad with 0's
date "+%A %-d`DaySuffix` %B %Y"

I have something similar working on a Linux machine (Ubuntu 8.10). I don't think it will work with Solaris, the one machine I tested did not allow using a _ character following the % to avoid padding the field with a 0. The non-padding allows date to return 1 instead of 01 (01st doesn't look right versus 1st).

I use a shell function (again, your OS or shell version may not like the way I defined the function) named DaySuffix, then call that func as part of the date call. The func itself is fairly hacky, I'm sure there is a better way to do this but it works for me. Note the special cases for 11, 12, & 13 - you've got to love the English language!

#!/bin/sh

DaySuffix() {
    if [ "x`date +%-d | cut -c2`x" = "xx" ]
    then
        DayNum=`date +%-d`
    else
        DayNum=`date +%-d | cut -c2`
    fi

    CheckSpecialCase=`date +%-d`
    case $DayNum in
    0 )
      echo "th" ;;
    1 )
      if [ "$CheckSpecialCase" = "11" ]
      then
        echo "th"
      else
        echo "st"
      fi ;;
    2 )
      if [ "$CheckSpecialCase" = "12" ]
      then
        echo "th"
      else
        echo "nd"
      fi ;;
    3 )
      if [ "$CheckSpecialCase" = "13" ]
      then
        echo "th"
      else
        echo "rd"
      fi ;;
    [4-9] )
      echo "th" ;;
    * )
      return 1 ;;
    esac
}

# Using consolidated date command from chris_l
# Also using %-d instead of %d so it doesn't pad with 0's
date "+%A %-d`DaySuffix` %B %Y"
拿命拼未来 2024-09-02 07:27:43
#!/bin/ksh
DateSuffix() {
if [ "$1" -eq "1" ] || [ "$1" -eq "21" ] || [ "$1" -eq "31" ]
then
echo 'st'
elif [ "$1" -eq "2" ] || [ "$1" -eq "22" ]
then
echo 'nd'
elif [ "$1" -eq "3" ] [ "$1" -eq "23" ]
then
echo 'rd'
else
echo 'th'
fi   
}
date "+%A %d`DateSuffix` %B %Y"
#!/bin/ksh
DateSuffix() {
if [ "$1" -eq "1" ] || [ "$1" -eq "21" ] || [ "$1" -eq "31" ]
then
echo 'st'
elif [ "$1" -eq "2" ] || [ "$1" -eq "22" ]
then
echo 'nd'
elif [ "$1" -eq "3" ] [ "$1" -eq "23" ]
then
echo 'rd'
else
echo 'th'
fi   
}
date "+%A %d`DateSuffix` %B %Y"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文