date and time comparison how to

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Oct 31 17:04:38 EDT 2012


Gary Herron wrote:
> On 10/29/2012 04:13 PM, noydb wrote:
> > All,
> >
> > I need help with a date and time comparison.
> >
> > Say a user enters a date-n-time and a file on disk.  I want to compare the date and time of the file to the
> entered date-n-time; if the file is newer than the entered date-n-time, add the file to a list to process.
> >
> > How best to do?  I have looked at the datetime module, tried a few things, no luck.
> >
> > Is os.stat a part of it?  Tried, not sure of the output, the st_mtime/st_ctime doesnt jive with the file's
> correct date and time.  ??
> >
> > Any help would be appreciated!
> 
> Use the datetime module (distributed with Python) to compare date/times.
> 
> You can turn a filesystem time into a datetime with something like the
> following:
>                  import datetime, os, stat
>                  mtime = os.lstat(filename)[stat.ST_MTIME]   // the
> files modification time
>                  dt = datetime.datetime.fromtimestamp(mtime)
> 

You could also write that as:

datetime.datetime.fromtimestamp( os.path.getmtime( path ) )


Ramit P


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list