<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Neil Poulin on Medium]]></title>
        <description><![CDATA[Stories by Neil Poulin on Medium]]></description>
        <link>https://medium.com/@NeilPoulin?source=rss-9bcd9cfbf567------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*k-SEsfPDdvJtL4bI.jpg</url>
            <title>Stories by Neil Poulin on Medium</title>
            <link>https://medium.com/@NeilPoulin?source=rss-9bcd9cfbf567------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 11:03:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@NeilPoulin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Encoding/Decoding Firebase Firestore Timestamp to Date with Swift 4.2 on iOS | Neil Poulin]]></title>
            <link>https://medium.com/@NeilPoulin/encoding-decoding-firebase-firestore-timestamp-to-date-with-swift-4-2-on-ios-neil-poulin-c83733f9f5a0?source=rss-9bcd9cfbf567------2</link>
            <guid isPermaLink="false">https://medium.com/p/c83733f9f5a0</guid>
            <category><![CDATA[timestamp]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[firestore]]></category>
            <dc:creator><![CDATA[Neil Poulin]]></dc:creator>
            <pubDate>Wed, 24 Apr 2019 05:04:58 GMT</pubDate>
            <atom:updated>2019-04-26T15:57:47.134Z</atom:updated>
            <content:encoded><![CDATA[<h3>Encoding/Decoding Firebase Firestore Timestamp to Date with Swift 4.2 on iOS</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w6sgnyxm68o1scMH9i1m2A.jpeg" /></figure><p><em>Note: The suggestions in this article were made in April 2019 using Swift 4.2 and XCode 10.2.1</em></p><p>The goal of this post is to give you an example of how to transform Firestore Timestamp to Swift&#39;s Date field in your project’s models and back again. I wrote it because I’ve searched the internet for example solutions to this and have only come up with bits and pieces.</p><p>For my approach, I wanted to avoid Firestore’s Timestamp type directly in the model because it causes a few design problems. For example, this keeps your models more lightweight and framework agnostic, as using Timestamp requires the import of FirebaseFirestore. Additionally, when preparing your views you will likely be converting from Timestamp to Swift’s Date object for formatting or arithmetic, and will never want to use Timestamp directly. So it doesn’t make sense to be passing around the heavy (because of the framework) intermediate object. It makes more sense to just keep a reference to the Date object on your model.</p><p>I also wanted to avoid writing code every time I needed to save/fetch data that converts Date objects to/from Timestamp objects. The ideal solution should be written once and be agnostic of the field name the date is stored under (i.e. I shouldn’t need to know ahead of time that dates are only stored under properties named createdAt, updatedAt, etc. – it should work even if I have a Date field with a silly, unconventional name like someUniqeFieldName).</p><p>What I wanted to accomplish is to have my models written as a struct (you can use a class if that works better for you) that conforms to the Codeable protocol, and can contain your standard (Codeable) model properties: strings, numbers, arrays, and, of course, Date. In my case, I aim to have my models map 1:1 with the fields I store in the Firestore document store.</p><p>Here are a few resources I found valuable as inspiration (although incomplete) for what I wanted to accomplish. I recommend checking out both if you are unsure about how the code provided below works. My solution is heavily inspired by both.</p><ul><li>Tutorial: <a href="https://www.youtube.com/watch?time_continue=1&amp;v=yfR2aBTuYwE">Encoding &amp; Decoding Firebase Snapshots</a> by Kilo Loco (Youtube)</li><li>Firebase Codeable: <a href="https://medium.com/@alickdikan/easy-custom-models-and-firebase-in-swift-4-with-codable-43bdb7805b31">Article</a> &amp; <a href="https://github.com/alickbass/CodableFirebase">CocoaPod Library on Github</a> by Alick Dikan</li></ul><h3>Cool… Just Give Me Some Code</h3><p>Here are the code snippets I’m using in my project. I tried to provide complete examples while pulling out stuff that wasn’t relevant to this discussion. Note: I have not complied or run the code in the gists on their own, although the bulk was literally copy-and-pasted from a working application. See inline comments for more information.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8bf9507c295cd3398a98514dd60b56fd/href">https://medium.com/media/8bf9507c295cd3398a98514dd60b56fd/href</a></iframe><p>Enjoy!</p><p><em>Originally published at </em><a href="https://www.neilpoulin.com/blog/encoding-decoding-firebase-firestore-timestamp-to-date-with-swift-4-2-on-ios/485"><em>https://www.neilpoulin.com</em></a><em> on April 24, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c83733f9f5a0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>