Conversion of String to Date Type

K

Keith Bolton

Hello All,

I've joined the wxPython list, but this is my first post on the python
list and it's good to be here :^) Anyway...

My question is relating to Date equality. I am checking if two string
representations of dates are less than or equal to eachother. The issue
that I am having is that the date strings, ex.
09/15/04 is evaluated as > 09/14/04. However, when the last two digits
for the year are changed, like this 09/15/04 is considered > 09/15/05.
I thought this had something to do with evaluating two strings. Could
this be the case, if so does someone know of a way to convert those
strings into a Date type object?

I look forward to hearing all of your feedback, thanks in advance.

Keith Bolton
 
B

Benjamin Niemann

Keith said:
Hello All,

I've joined the wxPython list, but this is my first post on the python
list and it's good to be here :^) Anyway...

My question is relating to Date equality. I am checking if two string
representations of dates are less than or equal to eachother. The issue
that I am having is that the date strings, ex.
09/15/04 is evaluated as > 09/14/04. However, when the last two digits
for the year are changed, like this 09/15/04 is considered > 09/15/05.
I thought this had something to do with evaluating two strings. Could
this be the case, if so does someone know of a way to convert those
strings into a Date type object?
String comparision doesn't know anything about the special semantics of
dates, and e.g. "09/15/04" is lexically before "10/15/03".
Possible solutions:
- use something like YYYYMMDD, YY/MM/DD... as string representation,
these will be sorted correctly (which is a good habit anyway, e.g. if
you have a bunch of files with a date in the filename)
- try "ts = time.mktime(int(s[6:8])+2000, int(s[3:5]), int(s[0:2]))" to
create timestamps and compare these. The datetime modules may have more
useful functions (haven't used it yet, time was sufficient for me).
- where do these strings come from? Perhaps there is an underlying
timestamp or datetime object that you could use instead of "string
representations".
 
A

Anna Martelli Ravenscroft

Hello All,

I've joined the wxPython list, but this is my first post on the python
list and it's good to be here :^) Anyway...

My question is relating to Date equality. I am checking if two string
representations of dates are less than or equal to eachother. The issue
that I am having is that the date strings, ex. 09/15/04 is evaluated as
like this 09/15/04 is considered > 09/15/05. I thought this had
something to do with evaluating two strings. Could this be the case, if
so does someone know of a way to convert those strings into a Date type
object?

Check out dateutil's fuzzy parser. It's great for handling this sort of
thing.

https://moin.conectiva.com.br/DateUtil#head-fe951d30a583390a274f57d3e881611e5ae207a5

HTH
Anna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,677
Latest member
MoisesKoeh

Latest Threads

Top