Unable to insert datetime object
See original GitHub issueEnvironment info Operating System: Windows 10 Python version: 2.7.12 gspread version: 2.0.0
Steps to reproduce
- worksheet.insert_row([datetime.datetime.now()])
Traceback:
Traceback (most recent call last):
File "D:/gspreadinserttest.py", line 21, in <module>
sh.insert_row([datetime.datetime.now()])
File "C:\Python27\lib\site-packages\gspread\v4\models.py", line 685, in insert_row
'values': [values]
File "C:\Python27\lib\site-packages\gspread\v4\models.py", line 112, in values_update
r = self.client.request('put', url, params=params, json=body)
File "C:\Python27\lib\site-packages\gspread\v4\client.py", line 67, in request
endpoint, json=json, params=params, data=data, files=files
File "C:\Python27\lib\site-packages\requests\sessions.py", line 566, in put
return self.request('PUT', url, data=data, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 494, in request
prep = self.prepare_request(req)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 437, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Python27\lib\site-packages\requests\models.py", line 308, in prepare
self.prepare_body(data, files, json)
File "C:\Python27\lib\site-packages\requests\models.py", line 458, in prepare_body
body = complexjson.dumps(json)
File "C:\Python27\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Python27\lib\json\encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Python27\lib\json\encoder.py", line 264, in iterencode
return _iterencode(o, 0)
File "C:\Python27\lib\json\encoder.py", line 178, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2018, 3, 11, 13, 26, 34, 292000) is not JSON serializable
I know it is possible to convert to a string before inserting however in previous gspread versions I could insert a datetime object and it would format the datetime using the formatting from the google sheet. Is there anyway this kind of functionality could be replicated in 2.0.0?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Unable to insert DateTime format into database - Stack Overflow
you try changing your date time formatDateTime theDateTime = DateTime.ParseExact(dateAndTime, "d MMMM yyyy hh:mm tt", provider); to DateTime ...
Read more >[Solved] can't insert datetime type to sql database - CodeProject
Check whether DateTime value, which you are inserting is in correct format or not? 3 solutions. Top Rated; Most Recent.
Read more >Cannot insert datetime value in datetime column - MSDN
Hi, I'm having a wierd problem. Tring to add datetime data to a simple table gives me the following error : Conversion failed...
Read more >Unable to Insert Date into Data Extension via SSJS
When retrieving dates via API, most of the time I end up with an array of objects. var array = [{"Status":1,"StatusMessage":"Complete"," ...
Read more >DateTime data insert to sql server - Studio - UiPath Forum
Hi, This is my input data: i try to insert data into sql server query… sometime date and time missing… i tried code: ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m not sure how helpful this will be, but I was able to write a date to the sheet by converting it to a count of the number of days since the Google Sheets epoch date of “1899-12-30” (https://developers.google.com/sheets/api/guides/concepts).
Something like:
cell.value = (datetime.datetime(2018, 3, 11) - datetime.datetime(1899, 12, 30)).days
You could use this function, it worked for me:
Imports:
Lets have a variable now as datetime object:
now = datetime.datetime.now()
then pass now to the following function as string.
This way your entered datetime will be recognized by google sheet as if it was entered by a person. If you want to know more about how to use gspread follow this link.
Entering multiple values could be done as well by replacing
[[str(now)]]
with:
[[str(now) ,'b','c','d']]