Date Time

Formatting data can be tricky especially when dealing with dates and times. Sometimes you’ll need to format the data to present just a month or year to the user other times you’ll need to format the user input to store in a database.

The Challenge

For this challenge we are working with dates and times. We’ll be formatting them to present to a user or display on a website. Many frameworks have tools to do this functionality for you but understanding the basics of how to format a date can help when your preferred framework doesn’t offer the functionality that you need.

The pattern for date time may vary depending on your language and framework. The universal sortable date time pattern is “yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’”. Where ‘yyyy’ is year, ‘MM’ is month, ‘dd’ is day, ‘HH’ is hours, ‘mm’ is minutes ‘ss’ is seconds, and ‘Z’ is the offset from UTC. For example 2017-02-25 08:30:00-6:00 is the time and day this meet up started February 25, 2017 at 2:30 pm CST.

Creating a Test Case

A Unit Test looks at and tests a specific unit of code be it a controller testing if it calls the right functions or a converter service. Writing Unit Tests can be broken down into three areas Arrange, Act, and Assert. The three A’s of unit testing. When you Arrange you set up your test cases for testing. This could be as simple as setting a variable to be passed in or complex like creating a large object or calling the database to get an existing item to update. When you Act you are doing the functionality that you are testing. This could be calling a method or checking that your database connections exist or your converter service is functioning. Finally you need to Assert that what you are doing is actually working.

No matter the level you choose to work with write a test case before you start your code. Then write just enough code to pass the test. Once you have passing code refactor to pass other test cases.

Some languages and frameworks have testing suites available to help build test driven apps. Here is a simple example of a test for this challenge written in JavaScript:

1
2
3
4
5
6
7
8
9
10
11
12
function TestCase1(){
//Arrange
var dateTime = "2017-02-25T08:30:00-06:00"
//Act
var month = getMonth(dateTime);
//Assert
if (month === "Feb"){
return "Test Passed"
} else {
return "Test Failed"
}
}

Level 1

Given a specific date write a function to return the month as a string. If the month is ‘02’ then the function should return ‘Feb’ or ‘February’. Next write a function to return the day of the week. So if the input is ‘2017-02-25’ the function will return Saturday.

A Step Further

Going a step further write a function that returns the time of day using the following table.

Time Ranges Time of Day
6am - 12pm Morning
12pm - 5pm Afternoon
5pm - 7pm Evening
7pm - 12am Night
12am - 6am Early Morning

Level 2

Given a task and how long it will take to complete write a function that returns the time or day and time the task will be complete if started immediately. For example if given the task ‘clean the kitchen’ and time ‘25 minutes’ return the current time plus 25 minutes.

A Step Further

Going a step further add another function for the user to note the time they finished the task. Then return the difference in how long it took to actually do the task as opposed to the predicted time.

Level 3

Given user input as a string with date information write a function to standardize the date information and return it in a format of your choice. The date could be represented in different ways. For example:

  • 25-Feb-2017
  • Feb,25,2017
  • 25/Feb/17
  • February 25, 2017
  • FEB/25/2017
  • 02/25/2017
  • 2017-02-25

A Step Further

Going a step further add a function to standardize the time then combine it with the standardized date to return a date time in the format of your choice. The time could be represented in different ways. For example:

  • 12pm
  • 2:30pm
  • Seven in the Morning
  • 12 noon
  • 1430
  • 08:30