Beta

Swedish National Identity Number

Description:

In Sweden a Personal Identity Number (Swedish: personnummer) is used in dealings with public agencies, from health care to the tax authorities. It is also used as a customer number in banks and insurance companies. It is written on all approved identity documents, making the risk of mixing up people low.

The number can be either

  • YYMMDDXXXX 10 digits or
  • YYYYMMDDXXXX 12 digits

It's usally separated by a - hyphen. Then it's formatted like this:

  • YYMMDD-XXXX
  • YYYYMMDD-XXXX

Where YYMMDD or YYYYMMDD is the year of birth.

Valid dates is between 1800-01-01 and 2099-12-31.

When the year is formated with two digits YY expect that the year is 19YY.

XXXX is divided into three parts. The first two are random, the third is also random but is odd for men and even for women.

The fourth digit is a control digit that checks if the complete number is valid.

To calculate this digit you take the first nine digits excluding the first 2 digits of the year if the number is twelve digits, and multiply each digit with 1 and 2 alternately.

After that you sum the digit sum of each answer and from that you subtract the sum with the nearest highest tenth of that sum. From that you get the last(fourth) digit.

Example

So if a girl is born January 4, 1897 the first part is 18970104. We take two random digits and append a random even integer 2.

We get the number 18970104792.

When we calculate the control digit we want to exclude the first two digits of the year, so we get the number: 19970104792

Multiplies each digit with 1 and 2 alternately

9  7  0  1  0  4  7  9  2
x  x  x  x  x  x  x  x  x
2  1  2  1  2  1  2  1  2

You get the results and sum them up.

1 + 8 + 7 + 0 + 1 + 0 + 4 + 1 + 4 + 9 + 4 = 39

Subtract 39 with the nearest highest 10 40 - 39 = 1 and you get the last control digit.

This leaves you with the complete identity number 9701047921

The problem

We want a function given a identity number to return true if it's valid and false if it's not valid.

Allowed formats for the parameter are

  • YYMMDDXXXX
  • YYMMDD-XXXX
  • YYYYMMDDXXXX
  • YYYYMMDD-XXXX

19YYMMDD or YYYYMMDD needs to be a valid date between 1800-01-01 and 2099-12-31

1800-01-01 and 2099-12-31 are valid dates.

If you encounter a 10 digit number, the first two digits are 19.

Make sure you check for leap years.

If the parameter is not correctly formated, it should return false.

Algorithms

Stats:

CreatedNov 23, 2016
PublishedNov 23, 2016
Warriors Trained57
Total Skips0
Total Code Submissions244
Total Times Completed17
JavaScript Completions17
Total Stars6
% of votes with a positive feedback rating58% of 6
Total "Very Satisfied" Votes2
Total "Somewhat Satisfied" Votes3
Total "Not Satisfied" Votes1
Total Rank Assessments6
Average Assessed Rank
6 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • phille97 Avatar
Ad