473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

date formatting

I want to write a script that Among other things) renames a file based on
it's timestamp.
I can get the date info using
"strftime('%y%m%d',localtime(os.stat('thefile' )[ST_MTIME])", but this seems
like a long way around the block. Is there a more direct way?

Bill
Jul 18 '05 #1
2 5417
WmGill wrote:

I want to write a script that Among other things) renames a file based on
it's timestamp.
I can get the date info using
"strftime('%y%m%d',localtime(os.stat('thefile' )[ST_MTIME])", but this seems
like a long way around the block. Is there a more direct way?


More direct way for what? os.stat() directly returns the time in seconds,
localtime() converts it to a nice tuple with year, month, day, etc., and
strftime() is a handy way to turn that into a string based on various
components.

If you don't want a string, you could of course stop before calling stftime(),
so that would be more direct. If you are certain you only want the year
month and day, you could skip the strftime() call and just do a % formatting
with '%04d%02d%02d' % localtime()[:3], but that's slightly less readable.

-Peter
Jul 18 '05 #2
No. I just thought there was a function to convert "time in seconds"
directly to "formatted string". Maybe it was some other language.

Thanks,
Bill

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:3F***************@engcorp.com...
WmGill wrote:

I want to write a script that Among other things) renames a file based on it's timestamp.
I can get the date info using
"strftime('%y%m%d',localtime(os.stat('thefile' )[ST_MTIME])", but this seems like a long way around the block. Is there a more direct way?
More direct way for what? os.stat() directly returns the time in

seconds, localtime() converts it to a nice tuple with year, month, day, etc., and
strftime() is a handy way to turn that into a string based on various
components.

If you don't want a string, you could of course stop before calling stftime(), so that would be more direct. If you are certain you only want the year
month and day, you could skip the strftime() call and just do a % formatting with '%04d%02d%02d' % localtime()[:3], but that's slightly less readable.

-Peter

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ed | last post by:
Hi there, My problem is the following: When I assign a custom formatted Date & Time to a Date variable it loses it’s formatting. Ex. 2005-06-07 15:46 now when I assign this to a variable of...
4
by: Paul | last post by:
Hi, Everything went fine getting a short date format out of SQL into my DataGrid with this: <%# DataBinder.Eval(Container.DataItem, "Created", "{0:d}")%> Then I got too fancy in SQL and...
4
by: dhnriverside | last post by:
Hi peeps I have a datepicker control that's providing dates in the format dd/mm/yyyy (UK). I want to convert this to "yyyy-mm-dd" to store as a text field in my database (had lots of problems...
12
by: Rob T | last post by:
I'm storing a date/time into a SQL table of type datetime. I need it to be precise so the value is stored to the 1000th of a second. ie "insert into myTable mydate values ('08/05/2005...
2
by: johndcal | last post by:
Hello All, I have a date value that I pull from a .csv file. After reading the file and storing the values in an array the value of the date could be found in $array, for example....
2
by: David Veeneman | last post by:
How does one format a date column in a GridView control? I had assumed that the DataFormat string would do it, but MSDN only shows numeric formatting codes. Can dates be formatted using that...
3
by: Jim in Arizona | last post by:
I have a gridview that's being populated from an access db query. The problem I'm having is that the date/time fields in access that are populating the gridview are showing both date and time, when...
21
by: Darin | last post by:
I have an applicatoin that works 100% perfect when running on a machine setup for English (United States), but when I change it to Spanish (Mexico), the dates start giving me fits. THe reason is...
7
by: creative1 | last post by:
Hello everyone. I am experiencing a strange problem that I can't fix on my own. I think I need expert's suggestions for this. The problem is: I want to print account statement (or any other...
9
by: Martin | last post by:
I'm retrieving some records from a database. One of the fields contains a date/time. I would like to format it as I send it out to the table in the displayed page. Can some one please tell me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.