Convert Date to Binary

Easy
1772.

Convert Date to Binary

Convert Date to Binary is an easy coding problem on strings and math, asked in Microsoft interviews.

You are given a string date in the format yyyy-mm-dd (a valid Gregorian date).

Write the year, the month and the day each in binary, without leading zeros, and join them with - in the same order: year-month-day.

Return that string.

Example 1

Input
date = "2080-02-29"
Output
100000100000-10-11101
Explanation
2080, 2 and 29 in binary are 100000100000, 10 and 11101.

Example 2

Input
date = "1900-01-01"
Output
11101101100-1-1
Explanation
Leading zeros of "01" disappear: 1 is just 1 in binary.

Example 3

Input
date = "2024-12-31"
Output
11111101000-1100-11111

Constraints

  • date.length == 10
  • date[4] == date[7] == '-'
  • every other character of date is a digit
  • date is a valid date between 1900-01-01 and 2100-12-31
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} date
 * @return {string}
 */
var convertDateToBinary = function(date) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Convert Date to Binary — Solution in Python, Java & C++

Convert Date to Binary solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Convert Date to Binary is an easy coding problem on strings and math, asked in Microsoft interviews.

  • Difficulty: Easy
  • Topics: Strings, Math
  • Asked at: Microsoft
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

You are given a string date in the format yyyy-mm-dd (a valid Gregorian date).

Write the year, the month and the day each in binary, without leading zeros, and join them with - in the same order: year-month-day.

Return that string.

Example 1

Input: date = "2080-02-29"
Output: 100000100000-10-11101
Explanation: 2080, 2 and 29 in binary are 100000100000, 10 and 11101.

Example 2

Input: date = "1900-01-01"
Output: 11101101100-1-1
Explanation: Leading zeros of "01" disappear: 1 is just 1 in binary.

Example 3

Input: date = "2024-12-31"
Output: 11111101000-1100-11111

Constraints

  • date.length == 10
  • date[4] == date[7] == '-'
  • every other character of date is a digit
  • date is a valid date between 1900-01-01 and 2100-12-31

Hints for Convert Date to Binary

  1. Hint 1

    Split the string on - into three decimal numbers.

  2. Hint 2

    Convert each number to binary — most languages have a built-in, or repeatedly take n % 2 and divide by 2.

  3. Hint 3

    Parsing the number first removes the leading zeros of "01"; join the three binary strings with -.

How to solve Convert Date to Binary

The date's three fields are independent numbers; converting each to base 2 and joining them is the whole task.

Approach

  1. Parse year = date[0..4), month = date[5..7), day = date[8..10) as integers.
  2. Write each in binary with no leading zeros (collect n % 2 while n > 0, then reverse).
  3. Return bin(year) + "-" + bin(month) + "-" + bin(day).

Why it works

Every field is at least 1, so its binary form is non-empty, and converting the parsed integer guarantees there are no leading zeros.

Complexity

  • Time — O(1)
  • Space — O(1)

Pitfalls

  • Converting the text "01" digit by digit would keep a leading zero; parse it as a number first.

Reference solution

Python

def convertDateToBinary(date: str) -> str:
    return "-".join(bin(int(part))[2:] for part in date.split("-"))

Java

public static String convertDateToBinary(String date) {
    String[] parts = date.split("-");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < parts.length; i++) {
        if (i > 0) sb.append('-');
        sb.append(Integer.toBinaryString(Integer.parseInt(parts[i])));
    }
    return sb.toString();
}

C++

string convertDateToBinary(string date) {
    auto bin = [](int v) {
        string r;
        while (v > 0) { r += (char) ('0' + v % 2); v /= 2; }
        reverse(r.begin(), r.end());
        return r;
    };
    int y = stoi(date.substr(0, 4)), m = stoi(date.substr(5, 2)), d = stoi(date.substr(8, 2));
    return bin(y) + "-" + bin(m) + "-" + bin(d);
}

JavaScript

var convertDateToBinary = function(date) {
    return date.split("-").map(function(p) { return parseInt(p, 10).toString(2); }).join("-");
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings