You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在旧版FreeBSD中修正日期参数格式化的脚本代码?

Fixing the Date Command for Old FreeBSD Systems

Got it, let's get that date formatting working for your old FreeBSD server. The issue here is that older versions of FreeBSD's date command uses a different syntax than newer FreeBSD releases or GNU date (the one you might be used to on Linux systems).

Your original command selected_date=$(date -j $1 +"%Y-%m-%d") fails because the old date requires you to explicitly define the input format of your date parameter using the -f flag when using -j for format conversion. Without -f, it tries to interpret your input as a system time to set (which isn't what you want, and will throw errors since you're not providing valid time-setting arguments).

Corrected Code

Assuming your input date parameter ($1) is in the format YYYY-MM-DD (e.g., 2024-05-20), here's the fixed command:

selected_date=$(date -j -f "%Y-%m-%d" "$1" +"%Y-%m-%d")

Let's break down what each part does:

  • -j: Tells date not to modify the system clock—we're only doing format conversion here.
  • -f "%Y-%m-%d": Specifies the format of the input date string you're passing in ($1). This must exactly match the format of your input parameter.
  • "$1": Your input date argument (always wrap this in quotes to avoid issues with spaces or special characters).
  • +"%Y-%m-%d": The output format you want (you can adjust this if you need a different format later).

Adjusting for Different Input Formats

If your input date uses a different format, just update the -f flag to match:

  • For input like 05/20/2024 (MM/DD/YYYY):
    selected_date=$(date -j -f "%m/%d/%Y" "$1" +"%Y-%m-%d")
    
  • For input like 20240520 (YYYYMMDD):
    selected_date=$(date -j -f "%Y%m%d" "$1" +"%Y-%m-%d")
    

Just make sure the -f format string aligns perfectly with how your input date is structured, and you'll be good to go!

内容的提问来源于stack exchange,提问作者Saeko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:33:55