Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking format of date and time inputs

Status
Not open for further replies.

kismet

MIS
Mar 5, 2003
24
PH
Hi! How can I check the format of an entered date and time in the command line? For example, I want the date format to be dd-mon-yyyy while the time, hh:mmT (with T as either a or p). Is there a command I can use that does these?

Thank you!
 
Not sure why you would want to do this, but you need to look at the man pages for the 'date' command. This can do what you want, but if this is for a script, why not have the user fill in the pertinent information for whatever function they are doing and then auto fill in the date/time in the format you would like it in, this way you don't have to validate it and it will save you quite a bit coding. Is this what you are looking for?
 
Hi pmcmicha!

Not really. I'm after the format check of the entered date in the command line. If an error in format it detected, the user is alerted.

I've checked the man pages for date but I didn't find anything that seems to answer my problem.
 
i dont't thnk that there swoul be any ready made comd to check the input. Besides, date would give output in a particulr format. What i understand hers is thta the user is entering the date.

so u have to chek it. using cut should give u three variables as dd, mm, yyyy and if they match i.e days is between 28/29/30/312 given the month and year



[ponder]
----------------
ur feedback is a very welcome desire
 
Thank you! I've figured out the date format already. The only thing left is the time. Since the user input input should be in hh:mmX format (ex. 10:30p), how can I cut this up in 3 sections? I have to cut this up since I have to convert the entered time into 24-hour basis (military time) that I can compare later on.

To give you more of an idea, given the user input is 10:30p (assuming that the user entered the time in correct format):
if ... # if last character is a "p" for pm
then
hour=firstfield #ex. 10
minute=secondfield #ex. 30
convertedtime=`echo $hour + 12 + $minute`
else # last character is a "a" for am
hour=firstfield ex. 10
minute=secondfield #ex. 30
convertedtime=`echo $hour + $minute`
fi

Any kind of help would greatly be appreciated!
 
To make it simple, you could require that the time be put in as military. But if you do not want to do this, then have them input the time as this:

10:30 p

Then you can cut the string based on the space delimiter or if you wanted to use this:

10:30p or
9:30a

Then you could run the input through an if statement where it could return the variable count and do a cut based on character length.

if [ ${#TIME} -eq 5 ]
then
AOP=`echo ${TIME}|cut -c5`
else
AOP=`echo ${TIME}|cut -c6`
fi

Of course this assumes that the user is keying in the time with a colon and no space for the 'a in am' or 'p in pm'. So you might want to add a some checks in for that as well.
 
it's good u hve solved atlest part of yr problem. pmcmicha has giv u time solution
u should shrae ur solution fo date with us



[ponder]
----------------
ur feedback is a very welcome desire
 
Thanks for the big help! I really appreciate it! =)
 
There's a little trick which works fine in Sun Solaris but not in AIX and should work fine also in SCO.
if you issue the command touch in order to modify the date if the syntax is not correct (if the data is not valid) you will have an error.
 
Try piping the user input through an awk script. See "man awk" for details (the awk man pages are well written and an excellent resource). I find you can do just about anything you want using awk, including splitting a string of input into separate parts and reformatting it, and also returning an error code if a format problem is identified. I have no doubt that awk can do what you are wanting, and there is no need for a space delimiter if you use awk.

Awk is an incredibly fast, and incredibly flexible data manipulation tool that can be used for simple one liners or managing entire databases. Awk uses simple C like syntax and can do if statements, loops, subroutines, string search and substring functions, multi-dimensional arrays, scientific arithmetic functions, multiple file data reads and writes, and system calls of anything else that isn't already included in the awk structure.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top