date and time comparison how to

Chris Angelico rosuav at gmail.com
Tue Oct 30 02:12:47 EDT 2012


On Tue, Oct 30, 2012 at 3:20 PM, noydb <jenn.duerr at gmail.com> wrote:
> But for the user supplied date... I'm not sure of the format just yet... testing with a string for now (actual date-date might be possible, tbd later), so like '10292012213000' (oct 29, 2012 9:30pm).  How would you get that input into a format to compare with dt above?

Instead of formatting your other date to match the input, turn the
input into something you can easily manipulate - preferably, Unix time
(seconds since 1970, sometimes referred to as a "time_t"). Once both
your timestamps are integer (or float) seconds since a known epoch,
they can be compared directly, as numbers.

You're already pretty much there with strptime. Poke around with that
and play with your format string and you should have it. But you may
need to tweak it to match your actual input.

ChrisA



More information about the Python-list mailing list