Working with bash and date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Working with bash and date
# 1  
Old 01-19-2011
Working with bash and date

Hello all,

I'm trying to substract 1 minute from the current date and take the hour and minute (for filename purpose).

1) If I want hour an minute from current time I can use:
Code:
timetmp=$(date +"%H:%M")

2) To substract 1 minute from current time I can use:
Code:
timetmp=$(date --date "$dte -1 minute")

Well now, how can I extract the minute and hour from example 2 like on example 1?

Could some1 please help me with this (i hope) simple trouble?
Thanks
# 2  
Old 01-19-2011
Code:
timetmp=$(date --date "$dte -1 minute" +"%H:%M")

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-19-2011
omg... I realize right now that it was very simple!!! Smilie

I used:
timetmp=$(date +%H%M --date "$dte -1 minute")

Btw, thanks Chubler_XL! Smilie
# 4  
Old 01-19-2011
BTW if you're doing a bit of date calculations a good trick is to convert your dates to seconds past epoch they you can add/subtract seconds as you like using shell, or even find the difference between two dates.

From date to epoch time date -d Jan-10-2011 +%s
From epoch time to date date -d @1294581600 +%b-%d-%Y
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 01-20-2011
If you have not GNU date, then ex. ksh93 is a solution:
Code:
epoc=$(printf "%(%s)T" "2010-10-24")
epoc=$(printf "%(%s)T" "2010-10-24 00:00:00")
epoc=$(printf "%(%s)T" "10/24/2010 00:00:00")
# day is 86400 s (60*60*24)
((yesterday=epoc-86400))
printf  "%(%Y-%m-%d)T"     "#$yesterday"

This User Gave Thanks to kshji For This Post:
# 6  
Old 01-20-2011
Guys, please help.
I make all my test under ubuntu and redhat 5 and all working fine.
The problem is that my script should run under SunOS where the bash version is:
GNU Bash-2.05

SunOS version is
SunOS rmcs 5.9 Generic_118558-09 sun4u sparc SUNW,Sun-Fire-V440

So the previous commands doesn't works. It show me the HHMM but it doesn't substract any time!!!

Code:
root@rms /# timetmp=$(date +%H%M --date "$dte -1 minute")
root@rms /# echo $timetmp
1307
root@rms /# date
Thu Jan 20 13:07:31 CET 2011

Is there any other commands??
# 7  
Old 01-20-2011
date under solaris doesn't have some of the nice features...

you could try:

Code:
#  date;perl -e 'print scalar(localtime(time()-60))."\n"'
Thu Jan 20 12:24:32 GMT 2011
Thu Jan 20 12:23:32 2011

or have a look at the FAQ: https://www.unix.com/answers-frequent...rithmetic.html

HTH
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Best way to get a bash script working in C

Ahoy friends. Currently i got a bash script running to manage my minecraft servers. All of them are stored in /home/minecraft_servers directory. Using my script im able to start a server (e.g. ./minecraft start ftb_continuum) because server name and server name are the same.(e.g.... (2 Replies)
Discussion started by: Knogle
2 Replies

2. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

3. Shell Programming and Scripting

Script to get last working date

Problem : we need a command that will give previous day date and previous day should be mon-fri only. Example :if we are running a command on Monday , it will give Friday ‘s date not Sunday’s . Few more restrictions : Command should be working in NM n/w. Hard coding of date should be avoided.... (11 Replies)
Discussion started by: Downsouth
11 Replies

4. UNIX for Dummies Questions & Answers

Date Validation not working

Hi Experts, I have a date validation script in that i will validate the date for a given format and search in the logs for that date. The script logic is very simple like below. Validate_Date() { is_valid=1 while do date_format=$(date "+$1") echo -e "Please enter the $2 date like... (6 Replies)
Discussion started by: senthil.ak
6 Replies

5. Shell Programming and Scripting

Date command is not working properly

Hi, in my script, i take the last month by a=$(date --date '1 month ago' +%Y%m) i expect that it give me in this month "March" as result 201402, but linux gave me 201403. IMPe@ABC123:> ~/date --date '1 month ago' +%Y%m 201403 i'm reasonably confused. Any idea? Thanks in advance, ... (2 Replies)
Discussion started by: IMPe
2 Replies

6. Shell Programming and Scripting

Working with grep and Bash

Hi, I am currently working on a Bash shell script that - Downloads a webpage, in this case youtube.com - Extracts Number of views, Extracts Title of video, Extracts User who made it, and lastly Duration. Then I have to Out put this into columns. To me this sounds like crazyness. I'm very new... (6 Replies)
Discussion started by: Njzangel
6 Replies

7. Shell Programming and Scripting

date command not working

hi #!usr/bin/perl -w local ($date) = `/sbin/date "+%D %X" ` ; print $date when i run this in ksh shell it is giving the below error sh: /sbin/date: not found but same code is working and displaying date and time in sh shell. what could be the reason. pls help (10 Replies)
Discussion started by: psthariharan
10 Replies

8. HP-UX

Why Bash is not working in HP-UX ?

Why Bash is not working in HP-UX ? What is similiar exe which is in HP_UX as Bash? (9 Replies)
Discussion started by: girija
9 Replies

9. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

10. Shell Programming and Scripting

working with date

I wan to convert any field having date in the file from the follwoing format 5/1/2003 to 2003-05-01. This file is tab delimited. Please help. (2 Replies)
Discussion started by: dsravan
2 Replies
Login or Register to Ask a Question