-
-
Save isu3ru/ea838c885e1337c83a48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Creates a Javascript Object with gender and date of birth | |
| * - extracted from a given valid Sri Lanka N.I.C. Number | |
| * isu3ru@gmail.com | |
| */ | |
| function parseNIC(nic) { | |
| var nicdata = { | |
| gender: '', | |
| dob: '' | |
| }; | |
| var pattern = /[0-9]{9}[V|X]/; | |
| if (!pattern.test(nic)) { | |
| nicdata.gender = ''; | |
| nicdata.dob = ''; | |
| return false; | |
| } | |
| var mon = { | |
| "1": ["Jan", 31], | |
| "2": ["Feb", 29], | |
| "3": ["Mar", 31], | |
| "4": ["Apr", 30], | |
| "5": ["May", 31], | |
| "6": ["Jun", 30], | |
| "7": ["Jul", 31], | |
| "8": ["Aug", 31], | |
| "9": ["Sep", 30], | |
| "10": ["Oct", 31], | |
| "11": ["Nov", 30], | |
| "12": ["Dec", 31] | |
| }; | |
| if (nic.length >= 5) { | |
| year = "19" + nic.substr(0, 2); | |
| days = parseInt(nic.substr(2, 3)); | |
| if (days > 500) { | |
| nicdata.gender = "Female"; | |
| days = days - 500; | |
| } else { | |
| nicdata.gender = "Male"; | |
| } | |
| var key; | |
| for (key in mon) { | |
| if (days > mon[key][1]) { | |
| days = days - mon[key][1]; | |
| } else { | |
| break; | |
| } | |
| } | |
| if (days < 10) { | |
| days = "0" + days; | |
| } | |
| nicdata.dob = mon[key][0] + ", " + days + " " + year; | |
| } | |
| return nicdata; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No support for new nic with 12 digits. only for the old 10 character NIC.