$date in collections (C#)

You can use DateTime for timezone-aware timestamps. If DateTimeKind is not specified, the client will use DateTimeKind.Utc for serializing/deserializing. DateTime values with kind DateTimeKind.Local are rejected for insertion due to timezone ambiguity.

You can also use DateOnly and TimeOnly, which are not timezone-aware.

The client also supports nullable versions of DateTime, DateOnly, and TimeOnly.

The following example uses dates in insert, update, and find commands:

using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Use date in an insertion
    var document = new Document()
    {
      { "registered_at", new DateTime(2020, 1, 1, 1, 1, 0) },
      { "hire_date", new DateOnly(2020, 1, 1) },
      { "shift_start", new TimeOnly(9, 15) },
    };

    await collection.InsertOneAsync(document);

    // Use date in a filter
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.And(
      filterBuilder.Eq(
        "registered_at",
        new DateTime(2020, 1, 1, 1, 1, 0)
      ),
      filterBuilder.Lt("hire_date", new DateOnly(2022, 11, 4))
    );

    await collection.FindOneAsync(filter);

    // Use date in an update
    var update = Builders<Document>
      .CollectionUpdate.Set("shift_start", new TimeOnly(10, 30))
      .Set("registered_at", new DateTime(2020, 3, 5, 1, 1, 0));

    await collection.UpdateOneAsync(filter, update);
  }
}

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | 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: Contact IBM