Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Document.write(变量.toDateString);不起作用_Javascript - Fatal编程技术网

Javascript Document.write(变量.toDateString);不起作用

Javascript Document.write(变量.toDateString);不起作用,javascript,Javascript,将工作,并将“测试”打印到文档中 document.write('test'); 什么也不做。d当然是我的日期变量,但它并没有像我想象的那样打印出日期。可能是业余爱好者的错误:(。我想用红色打印日期,我该怎么做 编辑:我的完整代码(我认为毫秒的转换使它的行为不正常,但我需要这样做。我如何将它全部转换回来或其他?) 很可能d不是一个真正的日期对象,而是其他对象 试试这个: var birthYear = parseInt(prompt ('Enter your birth year:')); v

将工作,并将“测试”打印到文档中

document.write('test');
什么也不做。d当然是我的日期变量,但它并没有像我想象的那样打印出日期。可能是业余爱好者的错误:(。我想用红色打印日期,我该怎么做

编辑:我的完整代码(我认为毫秒的转换使它的行为不正常,但我需要这样做。我如何将它全部转换回来或其他?)


很可能
d
不是一个真正的日期对象,而是其他对象

试试这个:

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.
d = d.getTime(); //Grabs the time of the user-entered 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 d=新日期();
document.write(“+d.toDateString()+”);

您在哪里初始化日期变量?并且,您是否在与“主”代码分开的文件中尝试了此操作,以便查看这是否是导致问题的代码?显示完整代码。您在哪个上下文中运行此操作?似乎合法:
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.
d = d.getTime(); //Grabs the time of the user-entered 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 d = new Date();
document.write("<span style=\"color: red;\">" + d.toDateString() + "</span>");