最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

datetime - Bash - Adding MinuteHour to the date fetched from database in string format - Stack Overflow

programmeradmin4浏览0评论

I am fetching a date in string format from database in the bash script. The format of the date is YYYY-MM-DD HH24:MI for instance,

date_string="2024-11-22 15:22"

when i try to add 1 minute to this, using the below command

ds=$(date -d "$date_string +1 minute" +"%Y-%m-%d %H:%M")

i get 2024-11-22 08:23

why is the hour getting changed. The timezone on the host is CST.

I would appreciate any insights or suggestions

I am fetching a date in string format from database in the bash script. The format of the date is YYYY-MM-DD HH24:MI for instance,

date_string="2024-11-22 15:22"

when i try to add 1 minute to this, using the below command

ds=$(date -d "$date_string +1 minute" +"%Y-%m-%d %H:%M")

i get 2024-11-22 08:23

why is the hour getting changed. The timezone on the host is CST.

I would appreciate any insights or suggestions

Share Improve this question edited Nov 27, 2024 at 0:12 Ed Morton 205k18 gold badges87 silver badges205 bronze badges asked Nov 26, 2024 at 22:54 Praneeth GudumasuPraneeth Gudumasu 1779 bronze badges 5
  • What do you get if you add %Z or %z to the datetime format? – choroba Commented Nov 26, 2024 at 23:57
  • 1 You added unnecessary complexity to your example by saving the date in a variable, then you added more unnecessary complexity by spawning a subshell to call date and saving the result in yet another variable. You could demonstrate the problem you're asking for help with using just date -d "2024-11-22 15:22 +1 minute" +"%Y-%m-%d %H:%M", no need for the additional distracting shenanigans. Always provide a minimal reproducible example, emphasis in this case on minimal. – Ed Morton Commented Nov 27, 2024 at 0:14
  • 6 date interprets +1 as UTC+1, then interprets the word minute to add one minute 15:23 and then converts result to CST by subtracting 7 hours, that is the difference between (UTC+1) and CST. For example, ... +2 minutes will result in 07:23 (that's because date assumes it's UTC+2 and subtracts 8 hours). Try explicitly stating timezone: ...="2024-11-22 15:22 CST" or ... CST +1 minute" + "%Y-.... – Man made of meat Commented Nov 27, 2024 at 1:30
  • Works for me with GNU date version 8.30. – Cyrus Commented Nov 27, 2024 at 4:39
  • explicitly adding the timezone worked. Thank You @user202311 – Praneeth Gudumasu Commented Nov 27, 2024 at 15:46
Add a comment  | 

1 Answer 1

Reset to default 0

Without knowing more about your environment, i.e. version of *nix, I tinkered around on my Windows git Bash. As to "why" it's doing it, we can only "guess" because *nix versions and flavors can vary in "subtle" ways.

On my git bash, running windows, I wrote the following script : As you can see, by adding the "today" to the ds1, it seems to work fine, so maybe try that.

#!/usr/bin/bash

date_string="2024-11-22 15:22"
echo "date_string : $date_string"
ds=$(date -d "$date_string +1 minute" +"%Y-%m-%d %H:%M")
echo "ds          : $ds"
ds1=$(date -d "$date_string today +1 minute" +"%Y-%m-%d %H:%M")
echo "ds1         : $ds1 "

Outputs 

$ ./test.sh
date_string : 2024-11-22 15:22
ds          : 2024-11-22 09:23
ds1         : 2024-11-22 15:23

hth,

Adym

发布评论

评论列表(0)

  1. 暂无评论