【问题标题】:How do I get the difference between two dates under Zsh?如何在 Zsh 下获得两个日期之间的差异?
【发布时间】:2021-08-27 03:04:23
【问题描述】:

任何人都知道如何将How do I get the difference between two dates under bash 提供的这个命令从 bash 转换为 zsh 命令

【问题讨论】:

  • 发布的 bash 命令无效,您可以查看是否只是将发布的内容复制并粘贴到 bash shell 中。您是在认真询问如何将不正确的 bash 命令转换为不正确的 zsh 命令吗?
  • 这是和以前一样的错误命令。它给了我一个 bash: let: DIFF=(date +%s -d 20120203-date +%s -d 20120115)/86400: syntax error: operand expected (error token is "%s -d 20120203-date +%s -d 20120115)/86400")。请上传屏幕截图,以便我们查看此命令在您的 bash 中的作用。
  • 您链接的命令与您发布的命令不同.....
  • 那是因为那些包含日期的符号

标签: shell date command zsh difference


【解决方案1】:

假设您需要多次执行此操作,我会为此创建一个可重用的函数。

  1. 将其放入名为duration-in-days 的文件中:
    autoload -Uz calendar_scandate
    local start end
    calendar_scandate $1        
    start=$REPLY        
    calendar_scandate $2        
    end=$REPLY        
    print $(( ( end - start ) / ( 24 * 60 * 60 ) ))
    
  2. 在您的.zshrc 文件中,autoload 您的函数。例如,如果您将函数文件保存在目录~/functions 中:
    # Autoload all functions in your ~/functions dir.
    autoload -Uz ~/functions/*~*.zwc
    # Exclude .zwc files (generated when you compile functions).
    
  3. 重新启动终端或 shell。

现在你可以像这样使用上面的函数了:

% duration-in-days 2012-01-15 2012-02-03
19
%

如果你想在一个可执行脚本中使用这个函数,那么记得在你的脚本中autoload它。

【讨论】:

    【解决方案2】:

    虽然 bash 和 zsh 完全不同,但它们非常相似,在您的情况下,相同的命令适用于两者:

    ((DIFF=($(date +%s -d 20120203)-$(date +%s -d 20120115))/86400))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      相关资源
      最近更新 更多