$date in collections

The handling of datetime objects changed in Python client version 2.0.

DataStax recommends upgrading to the latest version of the Python client. For more information, see Data API client upgrade guide.

The Python client provides the DataAPITimestamp and DataAPIDate objects for storing dates. However, if you insert a document with a DataAPIDate value, the client will always return DataAPITimestamp. DataStax recommends that you use DataAPITimestamp instead of DataAPIDate for collections since equality filters on DataAPIDate may not behave as expected if the implicit conversion to DataAPITimestamp uses a different timezone than expected. The DataAPITime and DataAPIDuration objects cannot be used with collections. For more information, see Python client usage: Client custom data types.

The Python client also supports standard-library date and datetime. If you use a standard-library datetime that is not timezone-aware (or just a date, which is always timezone-naive), the client will raise an error by default. For more information, see Python client usage: DataAPITimestamp and datetimes.

python
from astrapy import DataAPIClient
from astrapy.data_types import DataAPITimestamp
from datetime import datetime

# Get an existing collection
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")

# Insert documents containing timestamp values
collection.insert_one(
    {"when": DataAPITimestamp.from_string("2024-12-06T12:34:56.789Z")}
)
collection.insert_one({"registered_at": DataAPITimestamp.from_datetime(datetime.now())})

# Update a document, using a timestamp in the filter:
collection.update_one(
    {"registered_at": DataAPITimestamp.from_string("2024-12-06T12:34:56.789Z")},
    {"$set": {"message": "happy Sunday!"}},
)

# Update a document, setting "last_reviewed" to the current date:
collection.update_one(
    {"registered_at": {"$exists": True}},
    {"$currentDate": {"last_reviewed": True}},
)

# Find documents by inequality on a timestamp value:
print(
    collection.find_one(
        {
            "registered_at": {
                "$lt": DataAPITimestamp.from_string("2025-12-06T12:34:56.789Z")
            }
        },
        projection={"_id": False},
    )
)
# will print something like:
# {'registered_at': DataAPITimestamp(timestamp_ms=1733488496789 [2024-12-06T12:34:56.789Z])}

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax | Privacy policy | Terms of use | Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com