0

I have a datetime object got from a mysql database I found that its format is following the system datetime format. Therefore, the format constantly changes and it might contain other types of characters (depending on the system), which is very annoying.

Also, I can't use function like strftime to manipulate it.

I did a query from database:

results = myfunction.query("SELECT date FROM users WHERE id = 1")

and I got this [<System.DateTime object at 0x0000000000000080 [10/01/2020 13:14:56]>]

the column date is only a normal datetime column in a mysql database.

Then, I choose the 0th element of results:

result = results[0]

When I print the type of 'result', I got this <type 'DateTime'>

print type(result)

Then I use str() to convert result into a string.

print str(result)

I got this string '10/01/2020 13:14:56'

However, when I use another computer

I got this: 2020/10/01 下午 01:14:56 (probably following the windows default datetime format)

I am not able to manipulate the 'result' using strftime like:

result.strftime('Y-m-d')

My question is: How to get a unified format of the datetime in my case?

Ray Lee
  • 25
  • 3
  • What is the question? Also, note that an instance of class `datetime.datetime` does not have a *format*. – FObersteiner Oct 06 '20 at 09:30
  • Thanks for the reply. my question is how can i get the same datetime format everytime I query the database. Since the return datetime object follows the format of my windows system – Ray Lee Oct 06 '20 at 10:24
  • Again: a datetime object doesn't have a format. - so what do you mean by "format"? is it maybe a `str` actually? how does it look like? Please add a [mre]. – FObersteiner Oct 06 '20 at 10:33
  • @RayLee how are you getting the format? A datetime object is not a string, it has a default return type for when it is requested as a string which is the iso date format. Also, what are you doing so you can't use strftime? – IamFr0ssT Oct 06 '20 at 10:33
  • I get the datetime from a mysql database, and I print it using print function, it shows '06/2020/10 13:15:15'. I did some string operation on it like str() datetime.split('/') however, i got '2020/10/06 下午 01:15:15' when using another pc – Ray Lee Oct 06 '20 at 10:41
  • I am quite new to python. Not sure why this happens ='( – Ray Lee Oct 06 '20 at 10:42
  • 1
    @RayLee First check if you actually get datetime.datetime using `print(type(YOUR_VARIABLE_OR_RESPONSE))`, if it is `` then show more of the code. And again, datetime.datetime is not a string and printing it should print it in the iso datetime format. – IamFr0ssT Oct 06 '20 at 11:06
  • 1
    Please add more details *how* you obtain the date/time string (it's not a datetime object from your comments). Otherwise I don't think this question can get a precise and helpful answer. – FObersteiner Oct 06 '20 at 12:48
  • [] I got this from querying a mysql database. then I select the index 0 element. I got something like I then use str() to convert it. however, the format of this string changes when using different pc. sometimes I get something like [2020/10/01 下午 01:14:56] – Ray Lee Oct 06 '20 at 13:31
  • Please add this info to the question, not the comments. And also include the command you use for the query. – FObersteiner Oct 06 '20 at 13:47
  • I modify my question. Hope it is clearer now. Sorry for the misleading content T.T – Ray Lee Oct 06 '20 at 14:07
  • I don't know what System.DateTime is, that is not the python standard library datetime. What are you using as the mysql connector. I can't find anything about it in the official connector. I think this might be IronPython and it's database connector, that is crucial information. If it is check out [this question](https://stackoverflow.com/questions/6296841/how-do-i-convert-from-a-net-datetime-to-an-ironpython-datetime), your code would look something like `result = datetime.datetime(results[0])` – IamFr0ssT Oct 06 '20 at 14:32
  • You are a god ! It works perfectly, and you are right, I'm using ironpython! thanks again – Ray Lee Oct 06 '20 at 17:21

0 Answers0