CodeBlanks Logo Icon CodeBlanks

Date helpers

Add date processing functions.

Date helpers

Our third helper is a date formatter: formatDate. This method takes year, month and day values and returns a string in YYYY-MM-DD format. This ISO 8601 format is common in data exchange.

Example: static std::string formatDate(int year, int month, int day). In the body we convert numbers to strings with std::to_string. For single-digit months and days we prepend a zero: "0" + std::to_string(month). Result: Helpers::formatDate(2024, 3, 15) returns "2024-03-15". This method is useful for log entries and reports.

Conversion with std::to_string

To convert an int to a string, use std::to_string. Example: std::to_string(2024) returns "2024". You can join strings with +.

For single-digit months and days, a zero must be prepended. The logic: std::string m = std::to_string(month); if (m.length() == 1) m = "0" + m;. So 3 becomes "03", 12 stays "12". Finally we join the parts with hyphens: std::to_string(year) + "-" + m + "-" + d. This produces a deterministic output.

Practice Add the formatDate method and call it

Take the previous lesson's solution (Helpers class has capitalize and clamp). Under public: add static std::string formatDate(int year, int month, int day). In the body, prepend a zero for single-digit months and days, then return std::to_string(year) + "-" + m + "-" + d. In main after the clamp call add std::cout << std::endl << Helpers::formatDate(2024, 3, 15);.

Starter code

main.cpp
#include <iostream>
#include <string>
#include <cctype>

class Helpers {
public:
    static std::string capitalize(std::string text) {
        if (text.empty()) return text;
        text[0] = std::toupper(text[0]);
        return text;
    }
    static int clamp(int value, int min, int max) {
        if (value < min) return min;
        if (value > max) return max;
        return value;
    }
};

int main() {
    std::cout << "Ready" << std::endl;
    std::cout << Helpers::capitalize("hello") << std::endl;
    std::cout << Helpers::clamp(15, 0, 10);
    // Add the formatDate method and call it
    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <string>
#include <cctype>

class Helpers {
public:
    static std::string capitalize(std::string text) {
        if (text.empty()) return text;
        text[0] = std::toupper(text[0]);
        return text;
    }
    static int clamp(int value, int min, int max) {
        if (value < min) return min;
        if (value > max) return max;
        return value;
    }
    static std::string formatDate(int year, int month, int day) {
        std::string m = std::to_string(month);
        if (m.length() == 1) m = "0" + m;
        std::string d = std::to_string(day);
        if (d.length() == 1) d = "0" + d;
        return std::to_string(year) + "-" + m + "-" + d;
    }
};

int main() {
    std::cout << "Ready" << std::endl;
    std::cout << Helpers::capitalize("hello") << std::endl;
    std::cout << Helpers::clamp(15, 0, 10) << std::endl;
    std::cout << Helpers::formatDate(2024, 3, 15);
    return 0;
}

Expected output

Ready
Hello
10
2024-03-15

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match each call to its output

Term Meaning
formatDate(2024, 3, 15) "2024-03-15"
formatDate(2024, 12, 1) "2024-12-01"
formatDate(2024, 1, 5) "2024-01-05"
formatDate(2024, 10, 25) "2024-10-25"

2 Which is the ISO 8601 date format?

  • "15/03/2024"
  • "15-03-2024"
  • "03/15/2024"
  • "2024-03-15" Correct answer

Explanation: ISO 8601 is YYYY-MM-DD: 2024-03-15.

3 Complete the std::to_string call

Question
return std::____(year) + "-" + m + "-" + d;
Answer
return std::to_string(year) + "-" + m + "-" + d;

Explanation: std::to_string converts the number to a string.

All lessons in Intermediate