# Nest client. How to serialize date field to a custom format?

**URL:** <https://discuss.elastic.co/t/nest-client-how-to-serialize-date-field-to-a-custom-format/201562>\
**Category:** Elasticsearch\
**Created:** [September 30, 2019, 4:04am UTC](https://discuss.elastic.co/t/nest-client-how-to-serialize-date-field-to-a-custom-format/201562 "2019-09-30T04:04:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![einfoman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/einfoman/32/9186_2.png) [@einfoman](https://discuss.elastic.co/u/einfoman)\
**Post date:** [September 30, 2019, 4:04am UTC](https://discuss.elastic.co/t/nest-client-how-to-serialize-date-field-to-a-custom-format/201562/1 "2019-09-30T04:04:07Z")

</div>

Hi!  
I use Nest 7.3 and a default serializer. And I have a problem with date serialization.  
Suppose we have a model:

```auto
public class SearchEntity
{
    [Date(Format = "date_time_no_millis")]
    public DateTime Timestamp { get; set; }
}

```

I created index and a mapping of `timestamp` is OK.

Then I create an instance `SearchEntity` and try save it:

```auto
var entity = new SearchEntity { Timestamp = DateTime.Now };
client.Update(r => r
    .DocAsUpsert()
    .Doc(entity)
);

```

Here I get an error:

```auto
Type: mapper_parsing_exception Reason: "failed to parse field [timestamp] of type [date] in document with id '...'" CausedBy: "Type: illegal_argument_exception Reason: "failed to parse date field [2019-09-30T08:37:36.6356966+05:00] with format [date_time_no_millis]"

```

It doesn't matter, if I use date format `date_time_no_millis`, `yyyy-MM-dd` or any else, result the same.

What's wrong? How to set the field serialization format for DateTime property?

Thanks

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [October 4, 2019, 9:49am UTC](https://discuss.elastic.co/t/nest-client-how-to-serialize-date-field-to-a-custom-format/201562/2 "2019-10-04T09:49:51Z")

</div>

NEST client always serializes `DateTime`, `DateTimeOffset` POCO properties and their nullable counterparts, as ISO8601 strings. the `Format` property of the `Date` attribute does not influence how the `DateTime` or `DateTimeOffset` instance is serialized.

I've been thinking about ways in which `Format` could be supported, but there's a few things to consider:

- For simple date patterns where there is a direct mapping of the pattern understood by Elasticsearch to a pattern that would be understood by .NET date formatting, I _think_ this is the easiest case to handle.
- For _named_ date patterns understood by Elasticsearch, format patterns would need to be implemented in the client to serialize according to the _named_ pattern formats.
- POCOs that use attributes to specify `Format` would allow the creation of a custom `DateFormatter` when generating a formatter for the POCO through reflection, but `Format` can also be specified through fluent mapping. In this latter case, creating a custom `DateFormatter` is tricky because there may be no reflection metadata available to inform the format to use. Mapping could be fetched from an index but I can see several problems with this approach which I believe would make it untenable. There is the option of specifying the format with both fluent mapping _and_ an attribute but this doesn't feel like a great solution.

Open to ideas for how `Format` can be supported; more than [happy to discuss on an issue on the .NET client GitHub](https://github.com/elastic/elasticsearch-net/issues) repo 👍

One way to address formatting in the example provided for now would be to format the `DateTime` in the POCO to the desired format, and send the formatted value to Elasticsearch

```auto
public class SearchEntity
{
	private DateTime _timestamp;
	
	[Nest.Date(Name = "timestamp", Format = "date_time_no_millis")]
	public string TimestampString
	{
		get => _timestamp.ToString("yyyy-MM-dd'T'HH:mm:ss");
		private set => _timestamp = DateTime.ParseExact(value, "yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture);
	}
	
	[Ignore]
	public DateTime Timestamp
	{
		get => _timestamp;
		set => _timestamp = value;
	}
}

```

This would allow you to still work with `DateTime` in the POCO

```auto
    var client = new ElasticClient(settings);

    client.IndexDocument(new SearchEntity
	{
		Timestamp = new DateTime(2019,10,4,19,47,0)
	});

```

yields

```json
POST http://localhost:9200/index/_doc
{"timestamp":"2019-10-04T19:47:00"}

```

Ensure a `SearchEntity` can be deserialized from the output

```auto
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes("{\"timestamp\":\"2019-10-04T19:47:00\"}")))
{
    var doc = client.SourceSerializer.Deserialize<SearchEntity>(stream).Dump();
}

```

---

<div class="post-metadata">

**Author:** ![einfoman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/einfoman/32/9186_2.png) [@einfoman](https://discuss.elastic.co/u/einfoman)\
**Post date:** [October 6, 2019, 7:44pm UTC](https://discuss.elastic.co/t/nest-client-how-to-serialize-date-field-to-a-custom-format/201562/3 "2019-10-06T19:44:21Z")

</div>

@forloop thank you for the answer. I'll open an issue on github and use this workaround 👍

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 3, 2019, 7:46pm UTC](https://discuss.elastic.co/t/nest-client-how-to-serialize-date-field-to-a-custom-format/201562/4 "2019-11-03T19:46:02Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
