Skip to content

Instantly share code, notes, and snippets.

@nyancodeid
Created July 11, 2018 05:17
Show Gist options
  • Save nyancodeid/67f8c9477d74de7a2ad43c794a5d4066 to your computer and use it in GitHub Desktop.
Save nyancodeid/67f8c9477d74de7a2ad43c794a5d4066 to your computer and use it in GitHub Desktop.
Parse Nomor Induk Kependudukan (NIK) dengan Javascript
{
"nik": "XXXXXXXXXXXXXXXX",
"wilayah": {
"provinsi": "XX",
"kota": "XX",
"kabupaten": "XX",
"kecamatan": "XX"
},
"tanggal": {
"hari": "XX",
"bulan": "XX",
"tahun": "XXXX",
"lahir": "XXXX-X-X"
},
"uniq": "XXXX",
"_link": {
"_wilayah": "http://www.kemendagri.go.id/pages/data-wilayah"
}
}
function _parse(nomorNIK) {
nomorNIK = String(nomorNIK);
if (nomorNIK.length == 16) {
let thisYear = new Date().getFullYear().toString().substr(-2);
let thisCode = nomorNIK.substr(-4);
let thisRegion = {
provinsi: nomorNIK.substr(0, 2),
kota: nomorNIK.substr(2, 2),
kabupaten: nomorNIK.substr(2, 2),
kecamatan: nomorNIK.substr(4, 2)
}
let thisDate = {
hari: (nomorNIK.substr(6, 2) > 40) ? nomorNIK.substr(6, 2) - 40 : nomorNIK.substr(6, 2),
bulan: nomorNIK.substr(8, 2),
tahun: (nomorNIK.substr(10, 2) > 1 && nomorNIK.substr(10, 2) < thisYear) ? "20" + nomorNIK.substr(10, 2) : "19" + nomorNIK.substr(10, 2)
}
thisDate.lahir = new Date(thisDate.hari + "/" + thisDate.bulan + "/" + thisDate.tahun).toLocaleDateString();
return {
nik: nomorNIK,
wilayah: thisRegion,
tanggal: thisDate,
uniq: thisCode,
_link: {
_wilayah: 'http://www.kemendagri.go.id/pages/data-wilayah'
}
}
} else {
throw new Error(`Nomor NIK harus 16 digit`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment