LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-03-2014, 09:09 AM   #1
sravan14
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Rep: Reputation: Disabled
Comparing two dates


Guys,

Could you help me to get the logic to compare the 2 dates below:

d1="Feb 23 2015 12:00:00:000AM"
d2="Feb 23 2015 00:00:00:000AM"
 
Old 09-03-2014, 09:15 AM   #2
jbuckley2004
Member
 
Registered: Aug 2004
Distribution: Fedora (KDE spin)
Posts: 224

Rep: Reputation: 70
Generally, people here are not going to do your homework for you, srvan. But let me point you in the right direction.

First off, your question is a bit incomplete. "Compare" generally means that you're asking which of the two is larger. If that's all there is, the "logic" says, "look at the two strings, determine that they are dates, notice that the first is larger than the second and report your findings." You're done.

Oh? You wanted a program or a script to do all that? Interesting!
 
1 members found this post helpful.
Old 09-03-2014, 09:23 AM   #3
sravan14
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi jbuckley2004,

I was in a bit hurry to post my question.

My question is: How to recognize the two strings as dates? Once it is done I can compare two of them.

I tried using below code to recognise the string as date, but ended with errors.

$ date --date="Feb 23 2015 12:00:00:000AM"
date: invalid date `Feb 23 2015 12:00:00:000AM'

Thanks.
 
Old 09-03-2014, 02:18 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I would write a program or script to check the strings and first verify that they match the date format you require. Because dates can be formatted in a very large multitude of ways; such as mm/dd/yy or mm/dd/yyyy and things like the seconds could be the least significant value versus milli-seconds in your example. Also AM/PM versus 24-hour representation.

Therefore, first start by checking the strings to verify that they are valid date formats for what you expect. Likely part of that will be conversion of some of the numbers into numerical values versus strings as recognized by your program or script. But once you have validated that both are properly formatted dates, then write the next part which would be a function to compare: start with year, then month, then day; and verify also that the day is acceptable for instance there is no Feb 31st, ever. You can determine greater/lesser first via the date, but if the dates match, then you compare hours, minutes, seconds, and milli-seconds.

Write a program or script and start this effort. Once you've done something, and if you have problems accomplishing parts of it, post what you do have and then forum responders will likely offer suggestions.

The LQ policies are not that help won't be given for homework assignments, if that is what this is, but rather that you should make your own attempt first at solving your problem. And you should be truthful as to whether or not it is an assignment. You've been given suggestions how to approach the problem here. Start something and ask follow-up questions when you can post some more details.

Regarding being in a "hurry" to post. Slow down and review what you write before you hit submit. You can also edit things to be more clear about them.

Last edited by rtmistler; 09-03-2014 at 02:22 PM.
 
Old 09-03-2014, 02:27 PM   #5
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Which language are you intending to write your script in? Many will have built-in date parsing that will likely work better than using the 'date' command.
 
Old 09-03-2014, 02:38 PM   #6
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
also for 12:00 AM and 00:00 AM to be BOTH "true"
d1 is in 12 hour and d2 is in 24 hour
 
Old 09-03-2014, 10:25 PM   #7
jbuckley2004
Member
 
Registered: Aug 2004
Distribution: Fedora (KDE spin)
Posts: 224

Rep: Reputation: 70
Yes, and I was being a bit flipant too. Thanks for not taking offence.

You said:
Quote:
My question is: How to recognize the two strings as dates? Once it is done I can compare two of them.
And really, that depends entirely on the language you're using.

Were you trying to use the date command in a bash shell? That will only print or set the system time. You probably don't want to do that.

In C++, C, Perl, Javascript or that ancient, dead language I learned 40 years ago, Fortran, you'd either input the string (and then break the string up to get the separate parts) or assign the values to variables, validate them as being in the expected range, and then compare them largest to smallest (that is, compare the years first, then the months, then day, hr. min., sec.fractionsofseconds) and stop when you know when is larger.

Hope I'm not speaking down to you again. That's just a basic algorithm for this sort of thing. The rest of the details really do depend on the language.

Last edited by jbuckley2004; 09-03-2014 at 10:26 PM.
 
Old 09-04-2014, 01:41 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by sravan14 View Post
$ date --date="Feb 23 2015 12:00:00:000AM"
date: invalid date `Feb 23 2015 12:00:00:000AM'
Look at the message, then the manpage, then what format time should be in (hint - where's the decimal point ???).
Quote:
Were you trying to use the date command in a bash shell? That will only print or set the system time. You probably don't want to do that.
Why not pray tell ...
The date command will reformat datetimes quite nicely - even into seconds since epoch ... maybe the OP can take that as another hint.
 
Old 09-04-2014, 07:24 AM   #9
EmaRsk
Member
 
Registered: Mar 2006
Distribution: Mint, WSL Ubuntu
Posts: 134

Rep: Reputation: 32
http://www.fresse.org/dateutils/
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash command for comparing two dates Completely Clueless Linux - Software 6 03-25-2013 06:34 PM
[SOLVED] generate dates between two dates ip_address Programming 4 03-21-2013 04:28 PM
python: comparing dates stateless Programming 1 03-11-2013 04:25 PM
Comparing dates with ruby lmcilwain Programming 5 03-10-2007 02:29 PM
need to manipulate dates clsonnt Programming 5 08-19-2003 10:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:58 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