# add 2hours in current time

**URL:** <https://community.unix.com/t/add-2hours-in-current-time/211981>\
**Category:** Shell Programming and Scripting\
**Created:** [September 10, 2008, 10:40am UTC](https://community.unix.com/t/add-2hours-in-current-time/211981 "2008-09-10T10:40:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anup13](https://community.unix.com/letter_avatar/anup13/32/5_5575768a8748004e209b776fc1b2916d.png) [@anup13](https://community.unix.com/u/anup13)\
**Post date:** [September 10, 2008, 10:40am UTC](https://community.unix.com/t/add-2hours-in-current-time/211981/1 "2008-09-10T10:40:47Z")

</div>

hi  
i want to add 2hr ( or any hours inputed by user) to current time  
and print it.  
can any one help me with script.

eg suppose current date is "10-sep-2008 23:10:00"  
and if i added 4hrs to it output should be "11-sep-2008 03:10:00"

i want to run shell script in "ksh"

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [September 10, 2008, 10:58am UTC](https://community.unix.com/t/add-2hours-in-current-time/211981/2 "2008-09-10T10:58:50Z")

</div>

One way is to change the TZ variable in the script. example you are in MST (+7), pick a timezone two hours earlier like EST

```nohighlight
export TZ=MST7MDT
date
export TZ=EST5EDT
date

```

the 7 and the 5 are important you can make up 3 letter names for the timezone, or just reuse the current timezone. You can fudge time by as much as +12 -12 hours.

Otherwise see the datecalc script in Perderabo's thread 'Date Arithmetic' in the forums FAQ.

---

<div class="post-metadata">

**Author:** ![vidyadhar85](https://community.unix.com/user_avatar/community.unix.com/vidyadhar85/32/997_2.png) [@vidyadhar85](https://community.unix.com/u/vidyadhar85)\
**Post date:** [September 10, 2008, 11:38am UTC](https://community.unix.com/t/add-2hours-in-current-time/211981/3 "2008-09-10T11:38:30Z")

</div>

> [@anup13](#):
>
> hi  
> i want to add 2hr ( or any hours inputed by user) to current time  
> and print it.  
> can any one help me with script.
> 
> eg suppose current date is "10-sep-2008 23:10:00"  
> and if i added 4hrs to it output should be "11-sep-2008 03:10:00"
> 
> i want to run shell script in "ksh"

please post the output of your echo $TZ  
if you have date -d option you can do it with date also just go through the man page

---

<div class="post-metadata">

**Author:** ![otheus](https://community.unix.com/user_avatar/community.unix.com/otheus/32/505_2.png) [@otheus](https://community.unix.com/u/otheus)\
**Post date:** [September 10, 2008, 11:41am UTC](https://community.unix.com/t/add-2hours-in-current-time/211981/4 "2008-09-10T11:41:05Z")

</div>

the TZ solution is probably the simplest. In case you need something more arbitrary, perl comes in really handy, but this can also be done with a combination of date and bc.

```nohighlight
perl -e 'print "" . localtime(time()+$ARGV[0]) . "\n";' 7200

```

Here, 7200 represents 2 hours of seconds. The code above gets the current time in seconds, adds to it the contents of the first argument, and translates it back into standard "date" format.

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [September 10, 2008, 11:41am UTC](https://community.unix.com/t/add-2hours-in-current-time/211981/5 "2008-09-10T11:41:07Z")

</div>

If the OP has GNU date. The whole thing sounds like homework to me.

---

<div class="post-metadata">

**Author:** ![anup13](https://community.unix.com/letter_avatar/anup13/32/5_5575768a8748004e209b776fc1b2916d.png) [@anup13](https://community.unix.com/u/anup13)\
**Post date:** [September 12, 2008, 4:41am UTC](https://community.unix.com/t/add-2hours-in-current-time/211981/6 "2008-09-12T04:41:56Z")

</div>

hi  
guys  
thanks for your reply. !!

cant it done using only shell scripting. without using perl or doing changes to system like changing TZ

---

<div class="post-metadata">

**Author:** ![otheus](https://community.unix.com/user_avatar/community.unix.com/otheus/32/505_2.png) [@otheus](https://community.unix.com/u/otheus)\
**Post date:** [September 14, 2008, 2:55pm UTC](https://community.unix.com/t/add-2hours-in-current-time/211981/7 "2008-09-14T14:55:38Z")

</div>

> [@anup13](#):
>
> hi  
> guys  
> thanks for your reply. !!
> 
> cant it done using only shell scripting. without using perl or doing changes to system like changing TZ

Probably. Please read [the site rules.](http://www.unix.com/faq.php)

---

<div class="post-metadata">

**Author:** ![dennis.jacob](https://community.unix.com/user_avatar/community.unix.com/dennis.jacob/32/798_2.png) [@dennis.jacob](https://community.unix.com/u/dennis.jacob)\
**Post date:** [September 14, 2008, 3:33pm UTC](https://community.unix.com/t/add-2hours-in-current-time/211981/8 "2008-09-14T15:33:54Z")

</div>

Try the below one.

> [@](#):
>
> date -d "2 hour"

> [@](#):
>
> /home/ans \>date  
> Mon Sep 15 01:02:09 IST 2008  
> /home/ans \>date -d "2 hour"  
> Mon Sep 15 03:02:13 IST 2008

---

<div class="post-metadata">

**Author:** ![anup13](https://community.unix.com/letter_avatar/anup13/32/5_5575768a8748004e209b776fc1b2916d.png) [@anup13](https://community.unix.com/u/anup13)\
**Post date:** [September 15, 2008, 4:11am UTC](https://community.unix.com/t/add-2hours-in-current-time/211981/9 "2008-09-15T04:11:56Z")

</div>

> [@dennis.jacob](#):
>
> Try the below one.

i was trying to do same thing. but i thing on my machine date -d option is not available.

date: illegal option -- d  
usage: date [-u] mmddHHMM[[cc]yy][.SS]  
date [-u] [+format]  
date -a [-]sss[.fff]  
getting this thing  
i am using SunOS 5.7
