# Date as MapKey

**URL:** <https://forum.canton.network/t/date-as-mapkey/2030>\
**Category:** App Development\
**Created:** [February 2, 2021, 1:54pm UTC](https://forum.canton.network/t/date-as-mapkey/2030 "2021-02-02T13:54:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlyczak](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/mlyczak/32/1260_2.png) [@mlyczak](https://forum.canton.network/u/mlyczak)\
**Post date:** [February 2, 2021, 1:54pm UTC](https://forum.canton.network/t/date-as-mapkey/2030/1 "2021-02-02T13:54:32Z")

</div>

Hi,

I’d like to use a map where keys are Date instances.  
I understand that I need to write my own  
instance MapKey Date where…  
with definitions for ‘keyToText’, ‘keyFromText’.

What is the best way to implement ‘keyFromText’ given a Date instance?

Is there anything else I should include in such a MapKey definition?

Thank you very much for your guidance.

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [February 2, 2021, 2:16pm UTC](https://forum.canton.network/t/date-as-mapkey/2030/2 "2021-02-02T14:16:30Z")

</div>

From version 1.9 there is a new type `GenMap` in Beta, which can take keys of any serializable type. This feature will become stable with the upcoming 1.10 release (February 2021). If you are not tied to an SDK or Daml-LF version, I’d recommend simply using that.

Otherwise, you can use the functions [`daysSinceEpochToDate`](https://docs.daml.com/daml/stdlib/Prelude.html#function-da-internal-date-dayssinceepochtodate-81383) and [`dateToDaysSinceEpoch`](https://docs.daml.com/daml/stdlib/Prelude.html#function-da-internal-date-datetodayssinceepoch-33947) to convert between `Date` and `Int` and [`parseInt`](https://docs.daml.com/daml/stdlib/DA-Text.html#function-da-text-parseint-33239) and `show` to convert between `Int` and `Text`.

So in short:

```haskell
instance MapKey Date where
  keyToText = show . dateToDaysSinceEpoch
  keyFromText = daysSinceEpochToDate . fromSome . parseInt

```

---

<div class="post-metadata">

**Author:** ![mlyczak](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/mlyczak/32/1260_2.png) [@mlyczak](https://forum.canton.network/u/mlyczak)\
**Post date:** [February 2, 2021, 2:30pm UTC](https://forum.canton.network/t/date-as-mapkey/2030/3 "2021-02-02T14:30:28Z")

</div>

@bernhard Thank you very much for your prompt answer!

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [February 17, 2021, 1:30pm UTC](https://forum.canton.network/t/date-as-mapkey/2030/4 "2021-02-17T13:30:05Z")

</div>

> [@bernhard](#):
>
> If you are not tied to an SDK or Daml-LF version, I’d recommend simply using that.

Hey @bernhard, is there some special flag we need to pass to the compiler to enable this? I’m on `1.10.0-snapshot.20210209.6265.0.19bf4031`, but getting errors saying ` Module ‘DA.Map’ does not export ‘Map’.`

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [February 17, 2021, 1:31pm UTC](https://forum.canton.network/t/date-as-mapkey/2030/5 "2021-02-17T13:31:08Z")

</div>

SDK 1.10 still defaults to LF 1.8. You need LF 1.11 here. To enable that, add the following to your `daml.yaml`:

```haskell
build-options:
  - --target=1.11

```
