Hi, first of all, many thanks, I thinkl some of your suggestions help me depploy (successfully) my open-source project, so MANY THANKS TO THE OPEN COMMUNITY!!!
today’s affair: I need to compare dates (by two), this is, I need to compare 2 dates and determine “which happened before” .
this is, I have to compare 2 variables and find out which one “happened earlier” than the other, this means, which one is bigger, but regarding dates it is more difficult.
I would probably need to:
1)transform these dates to “date variables” (as we transform strings to integers)
2) would need to find a method that compares these dates, in terms of “which happened earlier”. I am not sure I can do this by using “greater than” or “less than”.
thanks to everybody!!
If you are using dates in a good format (per standards) you can just
compare strings:
2009-10-11 came after 2008-11-23 when comparing strings. We know that
because the format option is greater-than the latter regardless of how you
compare it (numerically or by string).
How are you getting your dates? In which format do you have the dates?
Which language are you using for getting the dates and which for
processing them? What is the purpose of knowing the date difference?
Good luck.
instaler wrote:
> Hi, first of all, many thanks, I thinkl some of your suggestions help me
> depploy (successfully) my open-source project, so MANY THANKS TO THE
> OPEN COMMUNITY!!!
>
> today’s affair: I need to compare dates (by two), this is, I need to
> compare 2 dates and determine “which happened before” .
> this is, I have to compare 2 variables and find out which one “happened
> earlier” than the other, this means, which one is bigger, but regarding
> dates it is more difficult.
>
> I would probably need to:
> 1)transform these dates to “date variables” (as we transform strings to
> integers)
> 2) would need to find a method that compares these dates, in terms of
> “which happened earlier”. I am not sure I can do this by using “greater
> than” or “less than”.
> thanks to everybody!!
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
“We know that
because the format option is greater-than the latter regardless of how you
compare it (numerically or by string).” got a bit confused here but I think what you mean is “greater-than” works fine for dates, regardless of them being integers or strings.
Dates’ format: YYYY-MM-DD:HH:MM:SS, but I can change the format, no problem with that, only thing is I have to compare till seconds.
I will get the dat from a 2-column list, so I can use either “for-list” or use awk, think awk could be better.
I am using simple bash-unix language and I do not need to know the difference, only know which one is greter.
It is a simple script, just to read an Excel sheet with calls (telephonic central)made at different times of the day/month/year.
With awk it’s much faster if used correctly and I can help you if you can give me an example of how the line looks like( i understand that the second field is the date) and what you are comparing with.
instaler wrote:
> I’ve used awk before and it is really great, yes, I just have to give it
> some thought. Anyway, here is part of the list:
>
> 2008-11-05 11:47:57 2008-11-05 11:51:39
> 2008-11-05 11:48:11 2008-11-05 11:55:41
> 2008-11-05 11:49:05 2008-11-05 12:01:28
> 2008-11-05 11:51:06 2008-11-05 11:53:24
> 2008-11-05 11:52:33 2008-11-05 11:54:42
> 2008-11-05 11:53:14 2008-11-05 11:56:07
> 2008-11-05 11:53:22 2008-11-05 11:54:41
>
> and I can also change format before using the script
> Thanks!!!
Convert the date to Unix time and compare.
$ $(date -d “2008-11-05 11:47:57” +%s) -lt $(date -d “2008-11-05 11:51:39” +%s) ]&& echo earlier || later
earlier
$ $(date -d “2008-11-05 11:51:39” +%s) -lt $(date -d “2008-11-05 11:47:57” +%s) ]&& echo earlier || echo later
later
Essentially you have 4 fields: date1 time1 date2 time2
Since all the fields are padded to full width, string comparison will yield the correct answer. In pseudo-code:
compare(date1,time1,date2,time2) {
if (date1 > date2)
return AFTER
else if (date1 < date2)
return BEFORE
else {
if (time1 > time2)
return AFTER
else if (time1 < time2)
return BEFORE
else
return SAME
}
}
I leave it to you to work out the actual code in awk, or whatever your preferred language is.