Add some specific time while using the linux command “date”

前端 未结 7 974
误落风尘
误落风尘 2021-01-07 20:18

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?

相关标签:
7条回答
  • 2021-01-07 20:27

    According this source you can just do:

    date --date='1 hour'

    0 讨论(0)
  • 2021-01-07 20:29

    In Linux shell script to add 1 hour or 60 minutes.

    dt=$(date -d '+ 60 minutes'  '+%FT%T.000Z')
    
    0 讨论(0)
  • 2021-01-07 20:30

    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.

    0 讨论(0)
  • 2021-01-07 20:33

    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
    
    0 讨论(0)
  • 2021-01-07 20:42

    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
    
    0 讨论(0)
  • 2021-01-07 20:46

    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

    0 讨论(0)
提交回复
热议问题