Add some specific time while using the linux comma

2020-04-02 07:49发布

In linux, date can help me to print the current time. If want to print the current time + 1 hour, what option should I give?

标签: linux date
6条回答
该账号已被封号
2楼-- · 2020-04-02 07:50

According this source you can just do:

date --date='1 hour'

查看更多
一夜七次
3楼-- · 2020-04-02 07:52

Linux and macOS in a single command

If you need to write scripts that work on both Linux servers and macOS workstations, you can silence the error of the first date call and 'OR' it (||) with the other. It doesn't matter which comes first.

date -u -d "+${max_age}Seconds" +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || \
date -u -v "+${max_age}S"       +"%Y-%m-%dT%H:%M:%SZ"

For example, this bash function uploads a file to AWS S3 and sets a Expires: and Cache-Control: headers.

s3_upload_cache_control(){
  local max_age_seconds="${1}" ;shift         # required
  local bucket_path="${1}"     ;shift         # required
  local filename="${1}"        ;shift         # required
  local remote_filename="/${1:-${filename}}"  # optional
  local fmt="+%Y-%m-%dT%H:%M:%SZ"
  aws s3 cp                                                          \
    "${filename}"                                                    \
    "s3://${bucket_path}${remote_filename}"                          \
    --expires                                                  "$(   \
      date -u -d "+${max_age_seconds}Seconds" $fmt 2>/dev/null  ||   \
      date -u -v "+${max_age_seconds}S"       $fmt               )"  \
    --cache-control max-age=$max_age,public                          \
    --acl public-read
}

查看更多
Rolldiameter
4楼-- · 2020-04-02 07:53

In shell script, if we need to add time then use below command and date format(PUT TIME before DATE string)

date -d"11:15:10 2017-02-05 +2 hours" +"%Y-%m-%d %H:%M:%S" this will output 2017-02-05 13:15:10

THis does not result in correct date without UTC it does not work

查看更多
\"骚年 ilove
5楼-- · 2020-04-02 08:01

TZ=Asia/Calcutta date should work (obviously this will get you the time in Calcutta).

This changes your TimeZone (TZ) environment variable for this operation only, it won't permanently set you to the new time zone you specify.

查看更多
兄弟一词,经得起流年.
6楼-- · 2020-04-02 08:01

We can get it using below code, i was handling a case wherein i required to traverse dates from last 3 months, i used below code to traverse the same

!/bin/sh

for i in {90..1}
  do
   st_dt=$(date +%F' 00:00:00'  -d "-$i days")
   echo $st_dt
   j=$((i-1))
   end_dt=$(date +%F' 00:00:00'  -d "-$j days")
   echo $end_dt
  done
查看更多
甜甜的少女心
7楼-- · 2020-04-02 08:05

On Linux

Just use -d (or --date) to do some math with the dates:

date -d '+1 hour' '+%F %T'
#    ^^^^^^^^^^^^

For example:

$ date '+%F %T'
2013-04-22 10:57:24
$ date -d '+1 hour' '+%F %T'
2013-04-22 11:57:24
#           ^

On Mac OS

Warning, the above only works on Linux, not on Mac OS.

On Mac OS, the equivalent command is

date -v+1H
查看更多
登录 后发表回答