Feb 7, 2014

Finding The Date of Birth Using NIC No and The Logic Behind The NIC No

In this blog post Ill explain you the logic behind the Sri Lankan National Identity Card (NIC). Sri Lanka now has Old NIC No Format as well as a New NIC Format.

First of will see, how this NIC No is constructed. Lets take a simple example.


OLD 901912350V
NEW 199019102350

Old Format

  • Old NIC Format consist of 9 numeric letters followed with alphabetic character.
  • Only first five digits contains the date of birth logic.
  • First two digits represents the born year. As you can see in the above example its "90". That means birth year is 1990.
  • Then next three numeric digits represent the no of days to your birthday date from January 1.


New Format
  • New NIC Format consist of 12 numeric letters only. 
  • Only first 7 digits contains the date of birth logic.
  • First four digits represents the born year. As you can see in the above example its "1990". That means birth year is 1990.
  • Then next three numeric digits represent the no of days to your birthday date from January 1.


But when calculating the month and date, you have to consider two things.
1. For all the years February month has 29 days.
2. For women's this three digits will start from 500. That means for women's this three digits has an additional 500.

Lets consider our example again. In my case its 191. So my birth month is July and date is 9th. So my birthday day is 1990-07-09. As I was mentioning above lets say a women born on the same day as our example. Then first five digits of her NIC No will be "90691" as per the old format and 
"1990691" as per the new format

Before moving to particular programming language lets see the logical writing of the code.

First of all you need to verify the NIC No. So we need to do few validations. 

  1. Length of the NIC No : To support both the new and old format it should be either 10 or 12 Characters long.
  2. If character length is 10, that means its old format. So first nine digits has to be numeric, followed by an alphabetic letter.
  3. Range : If old format, three digits after first two digits has be in the range of either 1 to 366 or 501 to 866. If new format range same for the three digits after first four digits


Now lets come to the interesting part. Lets calculate this birth day using a simple HTML + jQuery program. 


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html>
<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <title>Easy Code Stuff - NIC Birth Day Finder</title>
    <script>
        $(document).ready(function () {
            $("#find").click(function () {
                //Clear Existing Details
                $("#error").html("");
                $("#gender").html("");
                $("#year").html("");
                $("#month").html("");
                $("#day").html("");

                var NICNo = $("#nic").val();
                var dayText = 0;
                var year = "";
                var month = "";
                var day = "";
                var gender = "";
                if (NICNo.length != 10 && NICNo.length != 12) {
                    $("#error").html("Invalid NIC NO");
                } else if (NICNo.length == 10 && !$.isNumeric(NICNo.substr(0, 9))) {
                    $("#error").html("Invalid NIC NO");
                }
                else {
                    // Year
                    if (NICNo.length == 10) {
                        year = "19" + NICNo.substr(0, 2);
                        dayText = parseInt(NICNo.substr(2, 3));
                    } else {
                        year = NICNo.substr(0, 4);
                        dayText = parseInt(NICNo.substr(4, 3));
                    }

                    // Gender
                    if (dayText > 500) {
                        gender = "Female";
                        dayText = dayText - 500;
                    } else {
                        gender = "Male";
                    }

                    // Day Digit Validation
                    if (dayText < 1 && dayText > 366) {
                        $("#error").html("Invalid NIC NO");
                    } else {

                        //Month
                        if (dayText > 335) {
                            day = dayText - 335;
                            month = "December";
                        }
                        else if (dayText > 305) {
                            day = dayText - 305;
                            month = "November";
                        }
                        else if (dayText > 274) {
                            day = dayText - 274;
                            month = "October";
                        }
                        else if (dayText > 244) {
                            day = dayText - 244;
                            month = "September";
                        }
                        else if (dayText > 213) {
                            day = dayText - 213;
                            month = "Auguest";
                        }
                        else if (dayText > 182) {
                            day = dayText - 182;
                            month = "July";
                        }
                        else if (dayText > 152) {
                            day = dayText - 152;
                            month = "June";
                        }
                        else if (dayText > 121) {
                            day = dayText - 121;
                            month = "May";
                        }
                        else if (dayText > 91) {
                            day = dayText - 91;
                            month = "April";
                        }
                        else if (dayText > 60) {
                            day = dayText - 60;
                            month = "March";
                        }
                        else if (dayText < 32) {
                            month = "January";
                            day = dayText;
                        }
                        else if (dayText > 31) {
                            day = dayText - 31;
                            month = "Febuary";
                        }

                        // Show Details
                        $("#gender").html("Gender : " + gender);
                        $("#year").html("Year : " + year);
                        $("#month").html("Month : " + month);
                        $("#day").html("Day :" + day);
                    }
                }
            });
        });
    </script>
</head>

<body>
    <center>
        <p style="color: #000;">NIC Birth Day Finder</p>
        <p style="color: #000;">Both New & Old Format</p>
        <input type="text" id="nic" />
        <button id="find">Find</button>
        <br/>
        <br/>
        <p id="error" style="color: red;"></p>
        <p id="gender" style="color: #000;"></p>
        <p id="year" style="color: #000;"></p>
        <p id="month" style="color: #000;"></p>
        <p id="day" style="color: #000;"></p>
    </center>
</body>

</html>

Lets try this code in browser.





Hope you got what you are looking for.

5 comments:

Voxsar said...

the last digit of the nic for both old and new (old one with out the letter) is the check bit, do you know which algorithm is used to generated and validate this check bit

KWC Madhushanka said...

It's very usefull..thank you

Unknown said...

how can find last 3 digits of id number..explain please

Thilina Alagiyawanna said...

Thank you very much

Anonymous said...

It was really useful.
Thank you!

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...