Date command - subtract from given time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date command - subtract from given time
# 1  
Old 04-15-2014
Date command - subtract from given time

the given time is:

12:13:00


how do i subtract a 10 minutes from any given time?

Code:
date '12:13:00' '-10 min'

also tried this:

Code:
date +12:13:00 '-10 min'

# 2  
Old 04-15-2014
I don't think that the date command is intended for that. I can sort-of get it to work by abusing the timezone concept. Note that I had to switch signs.

Code:
$ date -u -d'12:13:00' '+%H:%M:%S'
12:13:00
$
$ date -u -d'12:13:00 +0010' '+%H:%M:%S'
12:03:00
$ date -u -d'12:13:00 -0010' '+%H:%M:%S'
12:23:00
$

This User Gave Thanks to Perderabo For This Post:
# 3  
Old 06-24-2014
It's probably too late to help SkySmart, but I thought that I would post a followup anyway. In another thread I saw Corona688 perform date arithmetic via the -d parameter to date. This led me to believe that time arithmetic should be possible too.

I had trouble getting it to work and all the problems centered around timezone computations. The first thing following the time is assumed to be a timezone offset. Therefore I had to supply a superfluous +0000 to get past that. Next I had to defeat the normal timezone computation and I used -u to do that. Putting it all together...
Code:
$
$ date  -u -d"12:13:00 +0000 -10 min" '+%H:%M:%S'
12:03:00
$ date  -u -d"12:13:00 +0000 +10 min" '+%H:%M:%S'
12:23:00
$

This User Gave Thanks to Perderabo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

2. Shell Programming and Scripting

Subtract a file's modification date with current date

SunOS -s 5.10 Generic_147440-04 sun4u sparc SUNW,SPARC-Enterprise Hi, In a folder, there are files. I have a script which reads the current date and subtract the modification date of each file. How do I achieve this? Regards, Joe (2 Replies)
Discussion started by: roshanbi
2 Replies

3. Shell Programming and Scripting

Subtract time in two line

INPUT: 16:45:51 10051 77845 16:45:51 10051 77845 16:46:52 10051 77846 16:46:53 10051 77846 Match the last PID then subtract second line time with first line. Please help me with any command or script. (3 Replies)
Discussion started by: vivekn
3 Replies

4. UNIX for Dummies Questions & Answers

How to chnage format and subtract time and date and get average.?

Hello All , Please support for below request how to change format and subtract time and date and get average. xxx 13-OCT-15 11.32.18.241000 AM 13-OCT-15 11.35.49.089080 AM xxx 13-OCT-15 11.32.24.000000 AM 13-OCT-15 11.45.17.810904 AM xxx 13-OCT-15 11.32.25.232000 AM ... (1 Reply)
Discussion started by: mirwasim
1 Replies

5. Shell Programming and Scripting

Subtract Seconds from Date Command

Hi, Need to subtract 5 seconds after syncing my Linux server from NTP like; #ntpdate time.myorg.int. This script will only run once in each morning at 9 AM. Please help me. (4 Replies)
Discussion started by: refra
4 Replies

6. Shell Programming and Scripting

Add/Subtract Time

need some help on the below requirement: File1: SV,22,20100501140000,JFK,RUH SV,29,20100501073000,BOM,RUH SV,29,20100501073000,SIN,RUH third filed is datetime which is of the format (yyyymmddhh24miss) File2 JFK,+,0500 BLR,-,0530 SIN,-,0800 for every line of file 1, take 4... (9 Replies)
Discussion started by: ssantoshss
9 Replies

7. Shell Programming and Scripting

subtract minutes from time

i have the time 20100421043335 in format (date +%Y%m%d%H%M%S),and i want to be able to get the previous time 2 minutes ago,which is 20100421043135 (9 Replies)
Discussion started by: tomjones
9 Replies

8. Shell Programming and Scripting

Subtract Time

Hello, Im writing a script using the ksh shell. I have 2 variables in the script: CURRTIME PREVTIME Example, if CURRTIME=13:00, I want to somehow calculate what the time was an hour ago so that PREVTIME=12:00 Right now I have the following: CURRTIME=`date +%H:%M` How can I... (4 Replies)
Discussion started by: xadamz23
4 Replies

9. UNIX for Dummies Questions & Answers

Subtract date & time in diferent rows

Hi Friends :) I have a long file having fields in the form : Field1 yy/mm/dd hh:mm:ss Duration(Sec) line 1) 123123 05/11/30 12:12:56 145 line 2) 145235 05/11/30 12:15:15 30 line 3) 145264 05/11/30 13:14:56 178 . . I want to subtract yy/dd/dd hh:mm:ss in line (2) from yy/mm/dd hh:mm:ss in... (1 Reply)
Discussion started by: vanand420
1 Replies
Login or Register to Ask a Question