python json load not able parse date format - python

i am trying to read json file with json load it throws error with date field (22-04-2000).for other fields which are strings i dont see any issue.seems there is parse issue with date format can someone help me on this?.i have tried placing that in String format too but still no luck.

There is no standard JSON representation of dates.
dates are stored in json in string format.
to push a date value to json object, first convert it to string.
var date = (new Date()).toString();
jsondata.push({"date": date});
to get date value back from json get the element then use
var date = new Date(jsondata.date);

Related

What's the correct datetime format for this string date generated by python?

I have this date example '2022-08-30T11:53:52.204219' stored in database, when I get it from database it's type is string so I wanted to convert it to a date type by using this python code
datetime.strptime('2022-08-30T11:53:52.204219', "%Y-%m-%d'T'%H:%M:%S.%f")
I also tried this one
datetime.strptime('2022-08-30T11:53:52.204219', "yyyy-MM-dd'T'HH:mm:ssZ")
But I always get this error response 'time data '2022-08-30T11:53:52.204219' does not match format "%Y-%m-%d'T'%H:%M:%S.%f'
I need help to convert this string date to an actual date
As per comment:
from datetime import datetime
print(datetime.strptime('2022-08-30T11:53:52.204219', "%Y-%m-%dT%H:%M:%S.%f"))
Result:
2022-08-30 11:53:52.204219

Python datetime formatting for yyyy-mm

Im have a csv file with date data which is formatted as 'yyyy-mm-dd' or 'yyyy-mm'.
When i import the csv file and convert to a dataframe, the datatype of my date column is object.
Im trying to convert this to datetime, but whenever i do, the dates which are formatted 'yyyy-mm' get converted to format 'yyyy-mm-dd'.
How can i convert the data from object to date but preserve the 'yyyy-mm' format?
Ive tried the methods 'pd.to_datetime', 'strptime' and 'strftime' which all result in my data changing to 'yyyy-mm-dd' format.
Thanks for your help

Converting a string iso format date field to actual iso format type to store in mongodb with python

I know there are so many questions and answers based on conversion of datetime field in python.
Here I am having a date field in the format like "2019-10-30T11:29:43+00:00", which is type string. I need to embed this value in to mongodb. Here the value is storing as a str in the database. I need this to be converted and store something like ISODate("2020-05-27T14:57:31.302Z").
So far I have tried is
date_iso = datetime.datetime.strptime(line['date_field'][:19], '%Y-%m-%dT%H:%M:%S');
date = date_iso.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
This returns the date in str type.
Note: This is python 2.7

Python: Reading Excel and automatically turning a string into a Date object?

I'm using the openpyxl library in Python and I'm trying to read in the value of a cell. The cells value is a date in the format MM/DD/YYYY. I would like for the value to be read into my script simply as a string (i.e. "8/6/2014"), but instead Python is somehow automatically reading it as a date object (Result is "2014-08-06 00:00:00") I don't know if this is something I need to fix in Excel or Python, but how do I get the string I'm looking for?
I would suggest changing it in your Excel if you want to preserve what is being read in by openpyxl. That said, when a cell has been formatted to a date in Excel, it becomes altered to fit a specified format so you've lost the initial string format in either case.
For example, let's say that the user enters the date 1/1/2018 into a cell that is formatted MM/DD/YYYY, Excel will change the data to 01/01/2018 and you will lose the original string that was entered.
If you only care to see data of the form MM/DD/YYYY, an alternate solution would be to cast the date with date_cell.strftime("%m/%d/%Y")
I found out how to fix it with these lines of code:
dateString = str(ws.cell(row=row, column=column).value.date())
newDate = datetime.strptime(dateString, "%Y-%m-%d").strftime("%m/%d/%Y")
The string "newDate" gives me the format "8/6/2018"

String to MMM-YY format

I am using xlrd to read a spreadsheet and write to a database. However, there is a cell value which needs to be written to a date column in the database.
The cell is a string and I read it as and trying to convert it to MON-YY as follows.
sales_month_val = curr_sheet.cell(1,5).value
print sales_month_val
current_sales_month = datetime.strptime(sales_month_val,'%MMM%-%YY%')
But I keep getting the conversion failed error message. Is the above conversion to datetime correct to convert to MON-YY format?
Thanks,
bee
You should take a look at this strftime reference.
The format you are looking for is:
%b-%y

Categories