How to work with datetimes betwixt javascript, JSON, and python, and mongodb?
There are obviously a lot of requirements to consider when dealing with time. However, in the case you want to maintain a Date/Time to be displayed to user in their time zone and use mongo/python/java/javascript, I've used ISO 8601 date formats and always store UTC (Zulu) time. Also, if you truly want to maintain the actual time that something occured, not only do you need to store the "date/time" you need to also store the "timezone" (for example IANA timezone string) where the event occurred.
For a lifetime a reading, you can search for "date time best practices". This answer has a good start on discussion: Daylight saving time and time zone best practices
Alright, now to the code (all of this can be found readily on the internet if you search for
" parse||output ISO 8601 parsing" (e.g. "python parse ISO 8601 date string"):
- JSON - on the wire between JavaScript and Python backend send a complex (can be simple if you have no need to preserve time zone) object with ISO-8601 formatted string and string to store time zone string:
{
"dateTime": "1970-07-10T12:00:00.047Z",
"timeZone": "America/New_York"
} Java Script
a. read datetime string JavaScript: Which browsers support parsing of ISO-8601 Date String with Date.parse
var d = Date.parse("2011-04-26T13:16:50Z");
b. write datetime string How do I output an ISO 8601 formatted string in JavaScript?
var date = new Date();
date.toISOString(); //"2011-12-19T15:28:46.493Z"Python
a. read datetime string How do I translate a ISO 8601 datetime string into a Python datetime object?
import dateutil.parser
yourdate = dateutil.parser.parse(datestring)
b. write datetime string Python - Convert date to ISO 8601
import dateutil.parser as parser
date.isoformat()- Mongo - store datetime as native datetime field http://docs.mongodb.org/manual/reference/mongodb-extended-json/#date
a. store datetime string (notice "$date" and that datetime is in ZULU/UTC (denoted by 'Z')):
"dateTime" : { "$date" : "1970-07-10T13:00:00.047Z"}
Reference:
1. IANA TimeZone Databasehttp://en.wikipedia.org/wiki/Tz_database
2. The google calendar API (event resource) can be used as an example for RFC 3339, as well (see start/end): https://developers.google.com/google-apps/calendar/v3/reference/events
json date and javascript date and finding the difference in days
First i would have a variable to convert the JSON obj date to date format by simply passing it as a string
var creationDate = new Date(this.creatorobject.creationdate);
ex : const d = new Date("2015-03-25");
then next find the difference between the 2 days ie: today - creationDate..
If they are 2 date formats then you get milliseconds. Convert milliSeconds to days and round it up to get a date.
var today = new Date();
var creationDate = new Date("2022-01-01");
var diff = today - creationDate;
const convertedDate = diff / (1000*60*60*24);
console.log(convertedDate);
How to overcome datetime.datetime not JSON serializable?
Updated for 2018
The original answer accommodated the way MongoDB "date" fields were represented as:
{"$date": 1506816000000}
If you want a generic Python solution for serializing datetime
to json, check out @jjmontes' answer for a quick solution which requires no dependencies.
As you are using mongoengine (per comments) and pymongo is a dependency, pymongo has built-in utilities to help with json serialization:
http://api.mongodb.org/python/1.10.1/api/bson/json_util.html
Example usage (serialization):
from bson import json_util
import json
json.dumps(anObject, default=json_util.default)
Example usage (deserialization):
json.loads(aJsonString, object_hook=json_util.object_hook)
Django
Django provides a native DjangoJSONEncoder
serializer that deals with this kind of properly.
See https://docs.djangoproject.com/en/dev/topics/serialization/#djangojsonencoder
from django.core.serializers.json import DjangoJSONEncoder
return json.dumps(
item,
sort_keys=True,
indent=1,
cls=DjangoJSONEncoder
)
One difference I've noticed between DjangoJSONEncoder
and using a custom default
like this:
import datetime
import json
def default(o):
if isinstance(o, (datetime.date, datetime.datetime)):
return o.isoformat()
return json.dumps(
item,
sort_keys=True,
indent=1,
default=default
)
Is that Django strips a bit of the data:
"last_login": "2018-08-03T10:51:42.990", # DjangoJSONEncoder
"last_login": "2018-08-03T10:51:42.990239", # default
So, you may need to be careful about that in some cases.
What is the right JSON date format?
JSON itself does not specify how dates should be represented, but JavaScript does.
You should use the format emitted by Date
's toJSON
method:
2012-04-23T18:25:43.511Z
Here's why:
It's human readable but also succinct
It sorts correctly
It includes fractional seconds, which can help re-establish chronology
It conforms to ISO 8601
ISO 8601 has been well-established internationally for more than a decade
ISO 8601 is endorsed by W3C, RFC3339, and XKCD
That being said, every date library ever written can understand "milliseconds since 1970". So for easy portability, ThiefMaster is right.
Is it possible to write date as DateTime format in JSON using Python without converting it to string
Simple answer is No, JSON does not have a native datetime representation, so it will appear as a string; however, if you use a standard that is agreed upon, the receiving application can parse the variable into a datetime object, if they choose to. If you do not know what format they may agree with, I would recommend just making it a standard ISO 8601(Combined date and time in UTC), that way it can be parsed by the receiver, and retain the correct value regardless of time zone.
Use JSON to communicate between Python and JavaScript to change CSS of a Website
Because jsondata
is defined within the definition of the callback function request.onload
while you are trying to access it outside the code block. You may want to move console.log
inside the onload function.
request.onload = function() {
var data = request.response;
var jsondata = JSON.parse(data);
return jsondata;
}
console.log(jsondata['text_06']); // jsondata is not accessible
It's also recommended that request.send()
is called after having request.onload
defined
request.onload = function() {
var data = request.response;
var jsondata = JSON.parse(data);
console.log(jsondata['text_06']);
return jsondata;
}
request.send();
If you want to access jsondata
outside, you may want to define a global variable outside the onload function and update it once you receive the data.
Related Topics
Tool for Checking Unused CSS Selectors
CSS Float Elements with Unequal Heights Left and Up in Grid
What Is the Most Efficient Way to Parse a CSS Color in JavaScript
Simplest Way to Set Cursor to Wait, Then Have All Elements Revert Back
How to Read the Applied CSS-Counter Value
Using JavaScript to Detect Google Chrome to Switch CSS
How to Get CSS3 Multi-Column Count in JavaScript
How to Have Tabs Without JavaScript
How to Apply CSS to Only Numbers in a Text Inside/Or Any <P><P> Element
How to Show Just the First Line of Text of a Div and Expand on Click
Good "Background-Size: Cover" Fallbacks/Shims/Tricks for Cross-Browser Compatibility on Divs
Jquery CSS Rendering - Works in Firefox, Not in Chrome
Positioning Multiple, Random Sized, Absolutely Positioned Elements So They Don't Overlap
How to Use CSSstylesheet.Insertrule() Properly