473,324 Members | 2,356 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,324 software developers and data experts.

Converting strings to dates

I'm trying to convert a string back into a datetime.date.

First I'll create the string:
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
import time, datetime
a_date = datetime.date.today()
s = str(a_date)
print s 2005-02-04

Now I convert it back:
new_date = datetime.date.fromtimestamp( .... time.mktime(time.strptime(s, '%Y-%m-%d'))) new_date

datetime.date(2005, 2, 4)
WOW, that's ugly. Is there a more concise way to do this?

-- Michael Chermside
This email may contain confidential or privileged information. If you believe you have received the message in error, please notify the sender and delete the message without copying or disclosing it.

Jul 18 '05 #1
2 4903
py> import time
py> date_str = time.strftime('%Y-%m-%d', time.localtime())
py> date_str
'2005-02-04'
py> time.strptime(date_str, '%Y-%m-%d')
(2005, 2, 4, 0, 0, 0, 4, 35, -1)

That work?

Jul 18 '05 #2
Chermside, Michael wrote:
I'm trying to convert a string back into a datetime.date.

First I'll create the string:
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more

information.
import time, datetime
a_date = datetime.date.today()
s = str(a_date)
print s 2005-02-04

Now I convert it back:
new_date = datetime.date.fromtimestamp( ... time.mktime(time.strptime(s, '%Y-%m-%d'))) new_date

datetime.date(2005, 2, 4)
WOW, that's ugly. Is there a more concise way to do this?


datetime.date(*map(int, s.split("-")))

Jul 18 '05 #3

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

Similar topics

0
by: max | last post by:
I am trying to access a foxpro database, using odbc all is well except that dates prior to 1970 are not returned as valid usable data, so would like to use ADO. When I select data containing...
1
by: ree32 | last post by:
I am having problems with Date Strings when using VS.net method of converting an XML document to a dataset. I need the the date nodes to be of date type. As I am loading the Dataset to a...
3
by: Scott Kinney | last post by:
I have a database that I imported from a csv file (I know, all the stories start out this way...) The dates were given as text strings of the form 'yyyymmdd', and were imported as text. Not all...
7
by: Dana Shields | last post by:
I am attempting to upsize from access to SQL Server. I'm trying to convert my queries to SQL Server views; however, I'm having a lot of difficulty with the syntax differences. For instance, a...
3
by: zshenk | last post by:
I need help converting dates. Currently, I have several tables and databases that use the format 1/20/2007. I have a table right now that uses the 20070120 format, and I was hoping that someone...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
2
by: HONOREDANCESTOR | last post by:
I've been converting a dotnet dll to a com object so that it can be called from vb6. If I want to pass a structure to a routine in the com object, like this: Call MyRoutine(byref MyStruct as...
3
by: Jef Driesen | last post by:
How can I convert a date string to a number (e.g. a time_t value or a tm struct)? I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying...
1
by: cloh | last post by:
I have a form in Access that generates an Excel Workbook with multiple worksheets. Each worksheet is unique to a particular location. The top row of each sheet shows the dates, the left-most column...
11
by: Keith Hughitt | last post by:
Hi, I am having a little trouble figuring out how to convert a python datetime to UTC. I have a UTC date (e.g. 2008-07-11 00:00:00). I would like to create a UTC date so that when I send it to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.