2012-03-21 80 views
0

我需要计算出某人有多少天,但我不确定数学。这是我到目前为止有:计算一个人有多少天?

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); 
+3

什么是你的问题? – 2012-03-21 09:11:22

+0

最佳做法是为此写一点测试:取三个或更多预定义的出生日期,并将它们与预定义的结果(http://www.timeanddate.com/date/duration.html)进行比较。这会更容易,因为使用警报和SO代码审查=) – 2012-03-21 09:22:12

回答

0

我不太确定究竟如何在JavaScript中实现这一点,但你为什么不基于用户的出生日期时间戳,从目前的减去它时间戳,然后(假设时间戳以秒为单位)将其除以一天中的秒数?

2

这实际上比这更容易。一旦你有两个日期(自己的生日和“现在”),你只需做到这一点:

var days = Math.floor((now - birthDate)/86400000); 

这是因为在JavaScript中,因为大纪元毫秒Date objects保持时间,当你对他们做数学,他们使用该毫秒值(并且结果是毫秒数)。而且JavaScript每天准确的假设为86,400,000毫秒。

所以一旦你有birthYearmonthNum,并birthDay

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

Live example using a datepicker | source

+0

这是有效的,但我也需要将该值“转换”为一个整数并将其写成蓝色为“您一直活着x天”。我忘了如何用js转换为整数。 – Chara 2012-03-21 09:19:38

+0

@Chara:该值已经是一个整数(JavaScript内部没有整数,所有数字都是浮点数),这就是我使用'Math.floor'的原因。我刚刚给答案添加了一个完整的例子,这应该有所帮助。 – 2012-03-21 09:30:53

+0

谢谢,它工作! – Chara 2012-03-21 09:35:15