ജാവാസ്ക്രിപ്റ്റിലെങ്ങെനെ പ്രായം ഗണിക്കാം
ഈ ജാവാസ്ക്രിപ്റ്റിൽ വയസ്സ് കണ്ടെത്തുന്നതെങ്ങനെയാണെന്ന് കാണണോ നിങ്ങൾക്ക് ?

കാണാം

// ജന്മ തിയ്യതി ആയിരിക്കണം ഇൻപുട്ട്
const calcAge = (dateString) => {
//ഇത് ഇന്നത്തെ തീയതിയുടെ മില്ലിസെക്കന്റുകൾ തരും
const birthday = new Date(dateString).getTime();
// ഒരു വർഷത്തിലെ മില്ലിസെക്കന്റുകൾ
const msInYear = 365.25*24*60*60*1000;
// ഇതാണ് വയസ്സ് ഗണിക്കുന്ന ഫങ്ങ്ഷൻ
return parseInt((Date.now() - birthday) / msInYear);
}
ആദ്യം ഇങ്ങനെ ഫങ്ങ്ഷൻ ഡിക്ലയർ ചെയ്യണം.

എന്നിട്ടിങ്ങനെ വിളിക്കണം
calcAge(“12 Jan 1999”)
അപ്പൊ 21 എന്ന് കിട്ടും!
2 Reactions


പിന്നെ ഒരു കാര്യം. ആ dateString
ഈ രൂപത്തിലൊക്കെ അടിക്കണം:
2020 02 01 (yyyy mm dd)
01 Feb 2020 (dd mmm yyyy)
1264962600000 (milliseconds since 1970)

ഈ dateString
ന്റെ ഫോർമാറ്റ് രേഖ കാണണോ? ഇതാണാ രേഖ:
ECMA 262 Specification

അയ്യേ . . .

പൂർണരൂപം
const calcAge = (dateString) => {
const birthday = new Date(dateString).getTime();
const msInYear = 365.25*24*60*60*1000;
return parseInt((Date.now() - birthday) / msInYear);
}
// calcAge("12 Jan, 1999") => 21
ഈ പോസ്റ്റിന്റെ ഡിസൈനിലേക്കു വിലപ്പെട്ട നിർദേശങ്ങൾ തന്ന അതുൽ സിറിയാക്കിനും ഗോപികൃഷ്ണനും ജിതിൻ ഷായ്ക്കും നന്ദി!
ഇനി വരാൻ പോകുന്ന പോസ്റ്റുകൾ ഉടനടി ലഭിക്കാനായി മേക്കേഴ്സ് ബ്രോഡ്ക്കാസ്റ്റ്വരിക്കാരാവുക
ഈ പോസ്റ്റ് ഇതുവരെ 1 തവണ വിതരണം ചെയ്തു.