$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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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);
}
}