LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2006, 04:14 AM   #1
Piotrekzc
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Rep: Reputation: 0
getting hour from date command


questioin is how to get current hour to variable, so i can increase it and use in script?

i need sth like that:

hour = getcurrenttime(only hour i need)
hour = hour + 1

some command [hour as a paramenter]


i dont want date to be changed, just want to get a number of hour

thx
 
Old 01-31-2006, 04:26 AM   #2
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
Did you read the man page?
Code:
man date
The synopsis of the command is this:
Code:
date [OPTION]... [+FORMAT]
The formats are extensive, but these 2 do what you request:
Code:
%k     hour ( 0..23)
%l     hour ( 1..12)
Thats 24-hour and 12-hour, respectively. Thus:
Code:
[scuzzy@slackdell /home/scuzzy]$ date
Tue Jan 31 05:38:17 EST 2006
[scuzzy@slackdell /home/scuzzy]$ date +%k
 5
[scuzzy@slackdell /home/scuzzy]$ date +%l
 5
[scuzzy@slackdell /home/scuzzy]$
 
Old 01-31-2006, 04:31 AM   #3
Piotrekzc
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Original Poster
Rep: Reputation: 0
thanks for help
 
Old 01-31-2006, 09:42 PM   #4
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
So the bash script would look like this:
Code:
#!/bin/sh
hour=`date +%k`
let "hour += 1"
echo "hour is $hour"
...is that right?
(I'm a real newbie, and am trying to learn bash scripting concepts myself - thanks for letting me eavesdrop)
 
Old 01-31-2006, 09:53 PM   #5
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by dogpatch
So the bash script would look like this:
Code:
#!/bin/sh
hour=`date +%k`
let "hour += 1"
echo "hour is $hour"
...is that right?
(I'm a real newbie, and am trying to learn bash scripting concepts myself - thanks for letting me eavesdrop)
in one shot!

Code:
echo "hour is $((`date '+%k'` + 1 ))"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Aergh. X dies on the hour, every hour l00zer Linux - Software 4 06-07-2005 10:02 PM
change clock from 24 hour to 12 hour in suse 9.2/KDE 3.3 jmlumpkin Linux - Newbie 1 01-22-2005 11:45 PM
date command aetucker1 Linux - General 3 01-21-2005 12:57 PM
mandrake 10 command line to gui boot, then stuck at boot with hour glass chris008 Linux - Laptop and Netbook 1 11-14-2004 05:29 PM
History command, How to get the date? philipina Linux - General 2 08-05-2004 05:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration