Javascript 计算出一个人有多少天大?

Javascript 计算出一个人有多少天大?,javascript,Javascript,我需要计算出一个人有多少天大,但我不确定他的数学。以下是我目前掌握的情况: var birthYear = parseInt(prompt ('Enter your birth year:')); var birthMonth = prompt ('Enter the name of the month of birth:'); var birthDay = parseInt(prompt ('Enter your day of birth as an integer:')); var mill

我需要计算出一个人有多少天大,但我不确定他的数学。以下是我目前掌握的情况:

var birthYear = parseInt(prompt ('Enter your birth year:'));
var birthMonth = prompt ('Enter the name of the month of birth:');
var birthDay = parseInt(prompt ('Enter your day of birth as an integer:'));
var milliDay = 1000*60*60*24; //Milliseconds in a day
monthAbb = 'janfebmaraprmayjunjulaugsepoctnovdec';
chineseZod = 12;
zodCycle = 1924; //Chinese Zodiac Cycle
var zodAnimal = new Array('Rat','Ox','Tiger','Rabbit','Dragon','Snake','Horse','Goat','Monkey','Rooster','Dog','Pig');
var zodAnimalD = new Array('Forthright, tenacious, intense, meticulous, charismatic, sensitive, intellectual, industrious, charming, eloquent, sociable, artistic, and shrewd.  Can be manipulative, vindictive, self-destructive, envious, mendacious, venal, obstinate, critical, over-ambitious, ruthless, intolerant, and scheming.','Dependable, ambitious, calm, methodical, born leader, patient, hardworking, conventional, steady, modest, logical, resolute, and tenacious.  Can be stubborn, dogmatic, hot-tempered, narrow-minded, materialistic, rigid, and demanding.','Unpredictable, rebellious, colorful, powerful, passionate, daring, impulsive, vigorous, stimulating, sincere, affectionate, humanitarian, and generous.  Can be restless, reckless, impatient, quick-tempered, obstinate, selfish, aggressive, and moody.','Gracious, good friend, kind, sensitive, soft-spoken, amiable, elegant, reserved, cautious, artistic, thorough, tender, self-assured, shy, astute, compassionate, lucky, and flexible.  Can be moody, detached, superficial, self-indulgent, opportunistic, and stubborn.','Magnanimous, stately, vigorous, strong, self-assured, proud, noble, direct, dignified, eccentric, intellectual, fiery, passionate, decisive, pioneering, artistic, generous, and loyal.  Can be tactless, arrogant, imperious, tyrannical, demanding, intolerant, dogmatic, violent, impetuous, and brash.','Deep thinker, wise, mystic, graceful, soft-spoken, sensual, creative, prudent, shrewd, elegant, cautious, responsible, calm, strong, constant, and purposeful.  Can be a loner, bad communicator, possessive, hedonistic, self-doubting, distrustful, mendacious, suffocating, and cold.','Cheerful, popular, quick-witted, changeable, earthy, perceptive, talkative, agile, magnetic, intelligent, astute, flexible, and open-minded.  Can be fickle, arrogant, childish, anxious, rude, gullible, and stubborn.','Righteous, sincere, sympathetic, mild-mannered, observant, artistic, intellectual, ingenious, innovative, creative, mothering, peaceful, and generous.  Can be indecisive, over-passive, worrier, pessimistic, sensitive, shy, and weak-willed.','Inventor, motivator, improviser, quick-witted, inquisitive, flexible, innovative, problem solver, self-assured, sociable, artistic, polite, dignified, competitive, objective, and factual.  Can be egotistical, vain, arrogant, selfish, reckless, snobbish, deceptive, manipulative, cunning, jealous, suspicious, and stubborn.','  Acute, neat, meticulous, organized, self-assured, decisive, conservative, critical, perfectionist, alert, zealous, practical, scientific, and responsible.  Can be over zealous and critical, puritanical, egotistical, abrasive, proud, opinionated, and gives into empty bravado.','Honest, intelligent, straightforward, loyal, sense of justice and fair play, attractive, amicable, unpretentious, sociable, open-minded, idealistic, moralistic, practical, affectionate, sensitive, and easy going.  Can be cynical, lazy, cold, judgmental, pessimistic, worrier, stubborn, and quarrelsome.','Honest, gallant, sturdy, sociable, peace-loving, patient, loyal, hard-working, trusting, sincere, calm, understanding, thoughtful, scrupulous, passionate, and intelligent.  Can be naive, over-reliant, self-indulgent, gullible, fatalistic, and materialistic.');
var monthArr = new Array(11);

monthArr [0] = "jan";
monthArr [1] = "feb";
monthArr [2] = "mar";
monthArr [3] = "apr";
monthArr [4] = "may";
monthArr [5] = "jun";
monthArr [6] = "jul";
monthArr [7] = "aug";
monthArr [8] = "sep";
monthArr [9] = "oct";
monthArr [10] = "nov";
monthArr [11] = "dec";

var monthNum = monthAbb.indexOf(birthMonth.slice(0, 3).toLowerCase()) / 3; 
alert(monthNum);
var d = new Date (birthYear, monthNum, birthDay);
alert(d);
var dCurrent = new Date();
dCurrent = dCurrent.getTime(); //Grabs the time of the current date in milliseconds.
var dTotal = dCurrent - d;
alert(dTotal);
dTotal = dTotal / milliDay;
dTotal = Math.floor(dTotal); //7193
alert(dTotal + ' is after division');
dTotal = dTotal / 365.25;
dTotal = Math.floor(dTotal);
alert(dTotal + ' is how old you are!');
dTotal = birthYear - zodCycle;
dTotal = dTotal % chineseZod;
alert(dTotal);
alert(d);
var testS;
testS = dCurrent - d;
testS = testS / milliDay;
testS = testS * 365.25;
document.write("<span style=\"color: red;\">" + 'Your birthday is ' + d.toDateString() + "</span>");
alert(testS);
var birthYear=parseInt(提示('输入您的出生年份:');
var birthMonth=prompt('输入出生月份的名称:');
var birthDay=parseInt(提示('以整数形式输入您的出生日期:');
var毫日=1000*60*60*24//一天几毫秒
monthAbb='Janfebmarapmayjunjulugsepoctnovdec';
chineseZod=12;
zodCycle=1924//十二生肖
var zodAnimal=新数组(“老鼠”、“牛”、“老虎”、“兔子”、“龙”、“蛇”、“马”、“山羊”、“猴子”、“公鸡”、“狗”、“猪”);
var zodAnimalD=新数组(“直率、顽强、强烈、细致、魅力四射、敏感、聪明、勤奋、迷人、雄辩、社交、艺术和精明。会操纵、报复、自我毁灭、嫉妒、虚伪、复仇、固执、挑剔、野心勃勃、无情、不容忍和诡计多端。”,“可靠、野心勃勃、冷静、有条不紊、天生领导者、耐心、勤奋、传统、稳重、谦虚、逻辑、果断、顽强。可以是固执、教条、脾气暴躁、心胸狭窄、物质主义、刻板、要求苛刻的人。“不可预测、叛逆、多姿多彩、强大、热情、大胆、冲动、活力、刺激、真诚、深情、人道和慷慨。be躁动、鲁莽、不耐烦、脾气暴躁、固执、自私、好斗、喜怒无常’,“亲切、好朋友、善良、敏感、说话温和、和蔼可亲、优雅、内敛、谨慎、艺术、彻底、温柔、自信、害羞、机敏、富有同情心、幸运和灵活。可以是喜怒无常、超然、肤浅、自我放纵、机会主义和灵活的顽固。,“宽宏大量、庄重、精力充沛、坚强、自信、骄傲、高贵、直率、端庄、古怪、聪明、热情、果断、开拓、艺术、慷慨和忠诚。可以是不得体、傲慢、专横、暴虐、苛求、不容忍、教条、暴力、冲动和鲁莽。,”深刻的思考者、睿智、神秘、优雅,说话温和,感性,富有创造性,谨慎,精明,优雅,谨慎,负责,冷静,坚强,持续,有目的。可能是一个孤独,沟通能力差,占有欲强,享乐主义,自我怀疑,不信任,虚伪,令人窒息,冷漠的人。,“开朗,受欢迎,机智,易变,朴实,感性,健谈,敏捷,有磁性,聪明聪明、机敏、灵活、思想开放。可能变化无常、傲慢、孩子气、焦虑、粗鲁、易受骗和固执。“正直、真诚、富有同情心、温文尔雅、善于观察、艺术、智慧、巧妙、创新、创造性、慈母、平和、慷慨。可能犹豫不决、过于被动、担忧、悲观、敏感、害羞和软弱illed.,“发明家、激励者、即兴发挥者、机智、好奇、灵活、创新、问题解决者、自信、社交、艺术、礼貌、端庄、竞争、客观和实事求是。可能自私、自负、傲慢、自私、鲁莽、势利、欺骗、操纵、狡猾、嫉妒、可疑和固执。”,细致、有条理、自信、果断、保守、批判、完美主义、机警、热情、务实、科学和负责。可能会过度热情和批判、清教徒、利己、粗暴、骄傲、固执己见,并虚张声势。“诚实、聪明、直率、忠诚、正义感和公平竞争,吸引性格开朗、友好、谦逊、善于交际、思想开放、理想主义、道德主义、务实、深情、敏感、随和。可能愤世嫉俗、懒惰、冷漠、判断、悲观、焦虑、固执、好斗。,“诚实、勇敢、坚强、善于交际、爱好和平、耐心、忠诚、勤奋、信任、真诚、冷静、理解、体贴”l、 谨慎、热情、聪明。可能天真、过度依赖、自我放纵、易受骗、宿命论和唯物主义。”;
var monthArr=新阵列(11);
monthArr[0]=“jan”;
蒙塔哈尔[1]=“二月”;
monthArr[2]=“mar”;
蒙特哈尔[3]=“4月”;
蒙塔哈尔[4]=“五月”;
monthArr[5]=“jun”;
蒙塔哈尔[6]=“七月”;
蒙塔哈尔[7]=“8月”;
蒙塔哈尔[8]=“九月”;
蒙塔哈尔[9]=“十月”;
蒙塔哈尔[10]=“11月”;
蒙塔哈尔[11]=“12月”;
var monthNum=monthAbb.indexOf(birthMonth.slice(0,3).toLowerCase())/3;
警报(月);
var d=新日期(生日、月份、生日);
警戒(d);
var dCurrent=新日期();
dcCurrent=dcCurrent.getTime();//获取当前日期的时间(以毫秒为单位)。
var dTotal=直流-d;
警报(dTotal);
dTotal=dTotal/milliDay;
dTotal=Math.floor(dTotal);//7193
警报(dTotal+‘在除法之后’);
dTotal=dTotal/365.25;
dTotal=数学楼层(dTotal);
警惕(dTotal+‘是您的年龄!’);
dTotal=出生年份-zodCycle;
dTotal=dTotal%chineseZod;
警报(dTotal);
警戒(d);
var检验;
试验=直流-d;
测试=测试/毫秒;
测试=测试*365.25;
document.write(“+”您的生日是“+d.toDateString()+”);
警报(测试);

我不确定如何在JavaScript中实现这一点,但是为什么不根据用户的出生日期生成一个时间戳,从当前时间戳中减去它,然后(假设时间戳是以秒为单位)将它除以一天的秒数?

这实际上要简单得多。一旦你有了两个日期(他们的生日和“现在”),你就可以这样做:

var days = Math.floor((now - birthDate) / 86400000);
这是因为在JavaScript中,从纪元开始以毫秒为单位保存时间,当你对它们进行计算时,它们使用毫秒值(结果是毫秒数),JavaScript假设每天正好86400000毫秒

所以一旦你有了
生日
,和
生日

var birthDate = new Date(birthYear, monthNum, birthDay);
var days = Math.floor((new Date() - birthdate) / 86400000);

|

可以,但我还需要将值“转换”为整数,并用蓝色写为“你已经活了x天”。我忘记了如何用js转换为整数。@Chara:值已经是一个整数了(JavaScript没有