Skip to content

Instantly share code, notes, and snippets.

@chivandikwa
Last active October 31, 2019 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chivandikwa/5c1080f0cf61dffd0ccf30754c2df6a3 to your computer and use it in GitHub Desktop.
Save chivandikwa/5c1080f0cf61dffd0ccf30754c2df6a3 to your computer and use it in GitHub Desktop.
DynamoDB nullable date property converter
using System;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;
public class NullableDatePropertyConverter : IPropertyConverter
{
public DynamoDBEntry ToEntry(object value)
{
DynamoDBEntry entry = new Primitive {
Value = null
};
if (value != null)
entry = new Primitive { Value = ((DateTime)value).ToString("o") };
return entry;
}
public object FromEntry(DynamoDBEntry entry)
{
var dateTimeString = (entry as Primitive)?.Value.ToString();
return DateTime.Parse(dateTimeString, null, System.Globalization.DateTimeStyles.RoundtripKind);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment