Chris Hannah Chris Hannah

Chris Hannah

Dates and DateFormatters in Swift

This article is part of a collection of articles about Swift Basics, where I try to explain different parts of Swift development in a more understandable way.


While doing some development in Swift, using Dates and DateFormatters, I found the task of going through every format option quite boring. So this is a quick “cheatsheet”, that anyone can use to identify what style of Date/Time they want to display, and also the code to get it.

Of course, you first need to create a Date object, which used in this way will generate the current date/time. Then you will also need a DateFormatter object, which handles the formatting, and is used to output the result into a usable String.

let date = Date()
let dateFormatter = DateFormatter()

Date

Full Date

Output: Wednesday, March 8, 2017

Code:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full
let stringOutput = dateFormatter.string(from: date)

Long Date

Output: March 8, 2017

Code:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
let stringOutput = dateFormatter.string(from: date)

Medium Date

Output: Mar 8, 2017

Code:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
let stringOutput = dateFormatter.string(from: date)

Short Date

Output: 3/8/17

Code:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
let stringOutput = dateFormatter.string(from: date)

Time

Full Time

Output: 1:26:32 PM Greenwich Mean Time

Code:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .full
let stringOutput = dateFormatter.string(from: date)

Long Time

Output: 1:26:32 PM GMT

Code:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .long
let stringOutput = dateFormatter.string(from: date)

Medium Time

Output: 1:26:32 PM

Code:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .medium
let stringOutput = dateFormatter.string(from: date)

Short Time

Output: 1:26 PM

Code:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .short
let stringOutput = dateFormatter.string(from: date)

Date and Time

You can of course, use the dateFormat and timeFormat together to output the date and time in the same string.

Here is an example:

Output: March 8, 2017 at 1:37 PM

Code:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .short
let stringOutput = dateFormatter.string(from: date)

Download

I’ve made a Swift Playground containing all of the formats for date and time, which you can use yourself by copying the code from GitHub.

The Original Apple Watch is a Steady Favourite

The very first Apple Watch was released 21 months ago, in April 2015. It was to me, a revolutionary new product, and I just had to get it. So I did.

In September 2016, 18 months after the original Watch was released, Apple then updated the original model, naming it Series 1, while also releasing a whole new version, Series 2.

You could argue that the Series 0 (first edition) to 1 change wasn’t even worth noting, the only main difference was that the Apple S1 processor used in the Watch was replaced with a dual-core variant. However in the Series 2, they added a newer Apple S2 processor which was also dual-core, but also an ambient light sensor, 50 m water resistance, 2x brighter display, and a GPS sensor.

So we can all agree that Series 0 → 2 was a decent update.

Therefore, you would expect that because of this, along with the fact that the Series 0 is a version 1.0 of a new product, that the need and desire to upgrade would be huge.

Except for me, it wasn’t. And it still isn’t.

My Watch

My Watch is a 42 mm Sport in Space Grey with the original matching Sport band, it still provides the same use as when I first purchased it, but now so much more.

At The Beginning

When I first started using the watch, the main feature that I used was the notifications. This was the major benefit for me, as it meant when I was on the move, be it walking or on the train for example, I didn’t have to keep taking my phone out of my pocket to find out what was trying to catch my attention.

Since then, I have rethought my take on how I manage notifications, but this has been a steady constant on the benefits of wearing the Apple Watch.

Of course, like everyone else, I thought the Apple Watch would be the kickstart I needed to become more active. I would start a new workout whenever I was walking somewhere, or maybe I’d try and jog, or maybe I would take an extra trip somewhere simply because I knew it was tracking me.

Then there was the apps, they took an awfully long time to load, and at the beginning they were run off the paired iPhone, so even at runtime they weren’t the best experience. Although in September 2015, Apple released watchOS 2, which meant that apps could be installed directly on the watch, and therefore would be faster. This was slightly the case, but it still wasn’t the best situation, as sometimes it would be much faster to take your phone out of your pocket, than it would be to just do the simple task on your wrist.

By wearing my watch more often, I was getting more comfortable with it, sometimes even forgot I was wearing it. Slowly I was realising that the things I was using my watch for, simply were being forced, and I didn’t actually want to do them. I just wanted to use the watch.

watchOS 3

With the release of watchOS 3 in September 2016, the Apple Watch was suddenly brought back to life.

This update brought significant increases to the speed of the device, which was helped out largely by the new Dock. By choosing to keep apps in the dock, the system would automatically keep them in memory, and therefore would be quicker to load. Alongside the speed benefits, there was also a bunch of new watch faces, and a huge overhaul to the way that the watch worked.

It also meant that because of these updates, developers were more likely to spend more time working on quality apps for the device, since they were able to provide a better experience for their users.

This really was the rebirth of the Apple Watch.

This Moment in Time

So right now, my watch has become a really essential part of my life, and it’s started to feel like it’s providing me with a use.
Here are the main things I now use my watch for:

  • Telling the time.
  • Notifications (Only for certain apps).
  • Checking what’s next in my calendar.
  • Weather.
  • Monitoring my activity and health.
  • Tracking my sleep.
  • Pokémon GO (yes I still play it).

Before I go into the “smart” side of the watch, it still is a watch, so I of course use it for the time. But very much like the iPhone, the “native” use of the device isn’t what brings people to use it.

Then there were the notifications, these have been narrowed down to the very few things that require my attention. So I have Messages, a select few email notifications from Airmail, and the occasional thing such as OneFootball when I want to specifically be updated about a game when I’m out doing something.

Recently I’ve been trying to use my calendar more and more, for university lectures, events, and time-sensitive events like flights. Because of this, knowing my next calendar event is essential, as the more I rely on the calendar as my primary schedule, I relinquish control over monitoring what I should be doing. Fantastical has a brilliant app for iOS, and I use their complication on the watch to check my next event, and with a simple tap I can quickly get a list of any other events in the future (made possible by the speed increases I watchOS 3).

It’s a similar situation to the weather, with DarkSky I can have a simple complication that updates super fast, so I can find out the weather in a few seconds.

In what has become a more silent feature of the watch, is the activity and health tracking. As I’m always wearing the watch, I get an accurate measurement of my activity throughout the day, how much I’ve walked, and of course my heart rate is also monitored. I don’t have any health conditions that I need to worry about, but it’s nice to know I have all of this data being logged without me doing anything.

In addition to the health tracking that is done automatically by the watch, by using an iOS app called AutoSleep, I can track the length and quality of my sleep by wearing my watch at night. It still of course needs to be charged, but as I’m sat at my desk, or when I’m not doing anything that involves moving around or needing to receive notifications, I pop it on my little watch stand, and it’s charges pretty quickly. The app uses a whole bunch of metrics to measure the sleep, such as the last time you used your phone, whether it’s on charge, or if you’re moving. But it’s the way in which you manage the sleep tracking is what got me, you just go to sleep. There’s no need to manually say “I’m going to sleep”, you just do it, and it knows. It’s the silent processes of the watch that makes me really love it.

So I’ve got the time, notifications, calendar, activity monitoring, and sleep tracking, there must be something I do on the watch for fun? Well, I do use the Pokémon GO watch app, in companion to the iOS game, in order to gain workout based rewards. It’s the closest to a game experience I have on the device, and it also doubles as a workout tracker, so there’s something else behind it as well.

I wouldn’t say there’s that much else that I use the watch regularly to do. Sometimes I control what I’m listening to, via the Music or Overcast app. As mentioned before, I use the OneFootball app to keep updated on certain football games. Once in a blue moon I use a timer, and that’s probably the only thing I use Watch Siri for. The only other thing I’ve been using is the iOS Wallet, which brings up my boarding passes whenever they’re needed.
When I look back at all of these activities that I now do on my watch, I can clearly see that it has become so much more than a time-telling, notification bringing device.

My Watch as an Appliance

Right now, I use my watch a lot. But even so, it’s not something I desperately need to get any better. I like it how it is.
With the benefits that came with watchOS 3, the device was given a new lease of life. Which made it feel like I’d already purchased a new watch.

I no longer need to worry about it syncing to my phone, whether the apps will be refreshed in time, or even about the speed of the hardware. It’s transformed to yet another computer that sits on my wrist, to a stable appliance that I can trust.

What I Still Don’t Like

Of course nothing is perfect, and I would like a few improvements to be made to the watch experience. But these are mainly down to the software, something that can be changed without the need for me to purchase a new device.

I really don’t like the grid layout of the apps, and I think that a redesign there would be a great improvement. But saying that, the way I interact with my watch is through complications via the watch face, or by launching apps from the dock.

I would also encourage the ability to customise the watch faces even further, so you would be able to have a more custom design and colour options.

The iPad is a More Efficient Writing Device

Can you do real work on an iPad?

Well if the real work is writing, then I argue that it is probably better at it than most other devices.

A writer’s provisions are the tools they use, and their imagination. This is why a lot of effort is put into finding and using the appropriate tools, because it allows the focus to be on the writing.

In my opinion, because of the writers need to focus on what they are writing, the best tools are the ones that don’t make themselves a distraction from the end goal. The ideal environment needs to adapt to the writer and not the other way around, which is where I think the iPad fits in perfectly. It’s of course, not a new product, but still the argument on whether work can be done on it successfully continues (although this seems to be dying out).

With the iPad, you have a fully portable device that is not only able to be taken anywhere, but it’s also able to be used anywhere. It also benefits from the consumerism around the device, because this has led to a wide array of accessories being made, such as external keyboards, stands, and now the Apple Pencil. Especially with iOS being such a flexible platform, and apps for pretty much every single scenario.

I have a MacBook Pro and while I like to write using it, I still find the iPad to be more suited to the job. With tasks like visually organising notes and ideas with the touch screen, or making use of the Apple Pencil, it’s as if you’re really interacting with the content, without any unnecessary distractions.

Also, it may sound strange, but the Workflow app for iOS is another reason for why I favour the iPad for writing, as it makes all the management tasks associated with writing much more streamlined and adaptable. The way I think of the comparison between writing on an iPad and writing on my Mac, is the ratio on which actual writing is done, compared to the time you spend managing your writing and the related process. For me, my Mac is okay for longer-form writing pieces where I spend a long time writing, that the amount of time editing, submitting to blogs, embedding images, and so on isn’t a big deal. On the other hand, with my iPad I have all of my processes automated with apps such as Workflow, Opener, Trello (Trello has a website you can use), so I can focus on the writing, regardless of the situation.

I of course can’t complain too much, because my Mac is the newest MacBook Pro, so it’s super light, and the keyboard is nice to type on. But I still believe the iPad edges out in front.

An iPad may take longer to get used to, and maybe a while to find and configure the writing tools for you. But once you’re there, you’re free to just write.

Imposter Syndrome and Failure

Nathan Jeffery on Imposter Syndrome and failure :

In a world where people like Elon Musk are literally building rockets and cars that run on energy from the sun it’s easy to look at your own life as an entrepreneur and feel like a bit of a failure or be totally dismissive when people acknowledge your success or compliment you. Sure maybe you’re not changing the world at the same scale as Elon but you are no doubt having an impact on countless people’s lives through job creation and that alone is commendable.

This was a nice little read, and I agree with everything he says.

Putting Your Xcode Project on GitHub the Easy Way

Warning: This guide is now outdated since Xcode has changed a lot since it was first published. A new version of this guide, Using GitHub and Xcode Together, is available and is based upon Xcode 10.1.

Developers are always talking about whether to use Git from the command line, or via a GUI such as GitHub Desktop. Well, if you’re using Xcode, then there’s a much simpler method that’s already built-in! It’s called Xcode Source Control[1].

I’m just about to upload one of my projects to GitHub, so I thought I’d share the process. Especially as I’ve recently found it so much easier to use, because it’s right there in Xcode!

Step 1

The first step is to let Xcode create a Git repository on your Mac. It should be checked by default.

Step 2

Then you will need to create a new repository on GitHub. Give it a name, description, and make it Public/Private. But make sure you don’t select “Initialize this repository with a README”.


Then select “Create Repository”.

Step 3

Then you’ll be redirected to a page where you’ll be asked to set up your repository. You only need one thing from this page, and it’s the URL in the Quick Setup section.

In my case, this is https://github.com/chrishannah/CH-Work-Item.git.

Step 4

Now it’s time to add this into Xcode.

Simply select Source Control from the menu bar, your project name, and then select the “Configure project” option.

Once you’ve done that, the next step is to select the “Remotes” tab, then select the + button in the bottom-left corner, and select “Add Remote”.

Here you will need to give it a name, and an address. The address is the URL previously found on the GitHub set up page. Then press “Add Remote”.

Step 5

The repository has now been created on GitHub, and added to Xcode, the next step is to initialise the repository.

Again, from the menu bar, select Source Control, and then Commit.

A window will appear with all the changes to the repository, since this hasn’t been initialised yet, it will show all of your projects files.

Simply add a commit message, select “Push to remote”, which should automatically select your GitHub repository, and press “Commit X Files and Push”.


If you haven’t connected your GitHub to Xcode before, you will be prompted for a username and password, these are your GitHub details.

That’s It!

Once you’ve completed all of the previous steps, your project should now be on GitHub, and if you go back to the repository on GitHub, it should now be populated.

If you haven’t already (like me above), it’s probably best to add a README file.

Managing Your Repository

From here it’s really easy to manage your repository in Xcode. It’s all found in the Source Control menu.

From there you can commit new files, push them to GitHub, pull any new changes, and anything else you’d expect from a Git client.


I hope that this guide proved useful, and provided a bit of piece of mind for you knowing that your project is now essentially backed up.

If you want to see the other tutorials I have here, then there is a Guide section.

You can find me on GitHub as chrishannah.


  1. For more information, there is a session from WWDC 2013, “Understanding Source Control in Xcode” that explains this in more detail ↩︎

How You Can Support Me

A bit of a weird post today, but I’ve come to the realisation that I need to find a way to fund myself through the last bit of my university course.

I spend nearly all of my time doing one of two things, university work, or developing things for everyone. Whether it’s a new application, a website, or something, I’m always making stuff.

I guess most people would simply raise the prices of their products, in order to try and make more money, but I don’t really want to do that. I want to reward people for backing what I do, so this is what I came up with:

Why would I make everything free, or really cheap? Because I trust everyone that finds my content useful, wants to help me in what I do, or simply is a super awesome person will back me.

And that’s why I’ve set up a Patreon page. Honestly I feel a bit dirty doing this, because I don’t want to ask for your money, but if I can balance this out by creating some awesome projects, then I think I can make sense of it.

If you can support me, then it really is a case of anything helps. The costs associate with University (especially in London) are stupid, and then there’s my desire to make things that also adds to that, even a coffee will help me through the day.


Don’t worry though, because I’ll have a good announcement in a few days relating to a certain app I’m making!

SpaceX to Send Privately Crewed Dragon Spacecraft Beyond the Moon next Year

We are excited to announce that SpaceX has been approached to fly two private citizens on a trip around the moon late next year. They have already paid a significant deposit to do a moon mission. Like the Apollo astronauts before them, these individuals will travel into space carrying the hopes and dreams of all humankind, driven by the universal human spirit of exploration. We expect to conduct health and fitness tests, as well as begin initial training later this year. – SpaceX Blog

Wow. 🌑

7 Names for 7 New Planets

With the recent discovery of the TRAPPIST-1 system, NASA asked on twitter for names for each of the planets:

Here are my suggestions:

  • Philosophi Lapis
  • Camera Secretorum
  • Vinctus Azkaban
  • Calix Ignis
  • Ordinis Phoenix
  • Dimidium Sanguinis Princeps
  • Letaliter Sanctio

Have a guess what they’re from!

Apple: Third-Party iPhone Screen Repairs No Longer Void Your Warranty

Joe Rossignol, writing for MacRumors:

iPhones that have undergone any third-party screen repair now qualify for warranty coverage, as long as the issue being fixed does not relate to the display itself, according to an internal memo distributed by Apple today. MacRumors confirmed the memo’s authenticity with multiple sources.

Finally, this should of been the case since the start.

Read the full article on MacRumors

The TRAPPIST-1 System

A NASA telescope(Spitzer Space Telescope) has discovered the largest batch of earth-size planets orbiting a single star, and three of them are in the habitable zone. In total there are seven planets, and they all are possible candidates of having liquid water, however the three located in the habitable zone are the most likely.

This discovery is not only fascinating, and incredibly breathtaking, but it also breaks a few records. With this being the first known system of seven Earth-size planets around a single star, and the three planets in the habitable zone are also the highest number found around the single star outside of our solar system.

The system, named TRAPPIST-1 after The Transiting Planets and Planetesimals Small Telescope, is about 40 light-years from Earth, but this is actually relatively close.

It will now be up to further observations to determine whether they contain liquid water, and also what their atmosphere is made up of.

I’m really excited to see what discoveries can be made in TRAPPIST-1, and maybe one of these will have the necessary ingredients needed for life to exist!

Find Out More: