0

I have his line in python. I would like it to output month and year as MM/YYYY. What is the best way to do this.

time.ctime(os.path.getmtime(os.path.join(dir_path, file_name)))

Current output:

Wed Sep 26 08:57:26 2012

I would like 9/2012

1
  • use time.strftime Commented May 13, 2016 at 21:52

2 Answers 2

2

time.ctime is documented to return a "string representing local time". It doesn't say what that string is (or how it's formatted), so I assume it's probably platform specific.

Instead, you'll probably want to use datetime.

secs = os.path.getmtime(os.path.join(dir_path, file_name))
date = datetime.date.fromtimestamp(secs)
formatted = date.strftime('%m/%Y')
Sign up to request clarification or add additional context in comments.

Comments

2
ts = os.path.getmtime(os.path.join(dir_path, file_name))
print(time.strftime('%m/%Y', time.gmtime(ts)))

2 Comments

I'm not sure about how you want to handle timezones here, but this will give you the month/year in GMT. If you want it in local time, I think that you need time.localtime(ts) instead of time.gmtime(ts).
I don't care too much about the time zone. My goal is to detect mxds (esri's map documents, ArcGIS) that have been modified in the last three years and if so I need to point all of the data sources to our new database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.