how2j.cn


工具版本兼容问题
JavaScript使用Date对象表示日期

关键字 简介 示例代码
new Date
创建日期对象
示例代码
getFullYear
getMonth
getDate
年/月/日
示例代码
getHours
getMinutes
getSeconds
getMilliseconds
时:分:秒:毫秒
示例代码
getDay
一周的第几天
示例代码
getTime
经历的毫秒数
示例代码
setFullYear
setMonth
setDate
setHours
setMinutes
setSeconds
修改日期和时间
示例代码
练习-计算年龄
示例代码
答案-计算年龄
示例代码
示例 1 : 创建日期对象   
示例 2 : 年/月/日   
示例 3 : 时:分:秒:毫秒   
示例 4 : 一周的第几天   
示例 5 : 经历的毫秒数   
示例 6 : 修改日期和时间   
示例 7 : 练习-计算年龄   
示例 8 : 答案-计算年龄   

示例 1 :

创建日期对象

通过new Date创建一个日期对象,这个对象就表示当前日期(现在)
"); window.frames["iframe_show1150"].document.write(decodeHtml($("textarea#stepcodeTextarea1150").val())); window.frames["iframe_show1150"].document.close(); $(window.frames["iframe_show1150"]).load(function(){ $("#iframe_show1150").height($("#iframe_show1150").contents().find("body").height()+showittryitheight); }); $("#iframe_show1150").height($("#iframe_show1150").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var d = new Date(); document.write('new Date():'+d); </script>
<script>
  var d = new Date();
  document.write('new Date():'+d);
</script>
"); window.frames["iframe1150"].document.write(decodeHtml(code1150)); window.frames["iframe1150"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1150"]).load(function(){ $("#iframe1150").height($("#iframe1150").contents().find("body").height()+showittryitheight); }); $("#iframe1150").height($("#iframe1150").contents().find("body").height()+showittryitheight); alreadyWriteCode1150 = code1150; $("#rendering1150").hide(); $("#rendered1150").show(); } var tRereshRetry2DemoPanel1150 = setInterval(rereshRetry2DemoPanel1150,1000); var binded1150 = false; $("textarea#stepcodeTextarea1150").keyup(function(){ if(!binded1150){ $(window).bind('beforeunload',function(){ binded1150 = true; return "xxxx"; }); } var newCode = $(this).val() code1150 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1150!=newCode){ // window.frames["iframe1150"].document.write("
"); // window.frames["iframe1150"].document.write(decodeHtml($("textarea#stepcodeTextarea1150").val())); // window.frames["iframe1150"].document.close(); // $(window.frames["iframe1150"]).load(function(){ // $("#iframe1150").height($("#iframe1150").contents().find("body").height()+showittryitheight); // }); // code1150 = newCode; // } }); $(".tryButton1150").click(function(){ $("#tryDiv1150").show(); $("#stepcodeTextarea1150").focus(); $("#stepcodeTextarea1150").height(200); $("#iframe1150").height(0); window.frames["iframe1150"].document.write("
"); window.frames["iframe1150"].document.write(decodeHtml($("textarea#stepcodeTextarea1150").val())); window.frames["iframe1150"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1150"]).load(function(){ $("#iframe1150").height($("#iframe1150").contents().find("body").height()+showittryitheight); }); $("#iframe1150").height($("#iframe1150").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1150.focus(); editor1150.setSize(null, "250"); $("#rendering1150").hide(); $("#rendered1150").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1150 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1150"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1150.on("change",function(doc){ if(!binded1150){ $(window).bind('beforeunload',function(){ binded1150 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1150 = newCode; $("textarea#stepcodeTextarea1150").val(newCode); if(alreadyWriteCode1150!=code1150){ lastModifedTime1150 = new Date().getTime(); $("#rendering1150").show(); $("#rendered1150").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1150 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1150.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1150").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

年/月/日

分别获取年/月/日
需要注意的是,getMonth()返回的月数,是基零的,0代表1月份
"); window.frames["iframe_show1151"].document.write(decodeHtml($("textarea#stepcodeTextarea1151").val())); window.frames["iframe_show1151"].document.close(); $(window.frames["iframe_show1151"]).load(function(){ $("#iframe_show1151").height($("#iframe_show1151").contents().find("body").height()+showittryitheight); }); $("#iframe_show1151").height($("#iframe_show1151").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var d = new Date(); document.write('分别获取年月日: '); document.write(d.getFullYear()); document.write("/"); document.write(d.getMonth()+1); document.write("/"); document.write(d.getDate()); </script>
<script>
  var d = new Date();
  document.write('分别获取年月日: ');
  document.write(d.getFullYear());
  document.write("/");
  document.write(d.getMonth()+1);
  document.write("/");
  document.write(d.getDate());
</script>
"); window.frames["iframe1151"].document.write(decodeHtml(code1151)); window.frames["iframe1151"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1151"]).load(function(){ $("#iframe1151").height($("#iframe1151").contents().find("body").height()+showittryitheight); }); $("#iframe1151").height($("#iframe1151").contents().find("body").height()+showittryitheight); alreadyWriteCode1151 = code1151; $("#rendering1151").hide(); $("#rendered1151").show(); } var tRereshRetry2DemoPanel1151 = setInterval(rereshRetry2DemoPanel1151,1000); var binded1151 = false; $("textarea#stepcodeTextarea1151").keyup(function(){ if(!binded1151){ $(window).bind('beforeunload',function(){ binded1151 = true; return "xxxx"; }); } var newCode = $(this).val() code1151 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1151!=newCode){ // window.frames["iframe1151"].document.write("
"); // window.frames["iframe1151"].document.write(decodeHtml($("textarea#stepcodeTextarea1151").val())); // window.frames["iframe1151"].document.close(); // $(window.frames["iframe1151"]).load(function(){ // $("#iframe1151").height($("#iframe1151").contents().find("body").height()+showittryitheight); // }); // code1151 = newCode; // } }); $(".tryButton1151").click(function(){ $("#tryDiv1151").show(); $("#stepcodeTextarea1151").focus(); $("#stepcodeTextarea1151").height(200); $("#iframe1151").height(0); window.frames["iframe1151"].document.write("
"); window.frames["iframe1151"].document.write(decodeHtml($("textarea#stepcodeTextarea1151").val())); window.frames["iframe1151"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1151"]).load(function(){ $("#iframe1151").height($("#iframe1151").contents().find("body").height()+showittryitheight); }); $("#iframe1151").height($("#iframe1151").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1151.focus(); editor1151.setSize(null, "250"); $("#rendering1151").hide(); $("#rendered1151").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1151 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1151"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1151.on("change",function(doc){ if(!binded1151){ $(window).bind('beforeunload',function(){ binded1151 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1151 = newCode; $("textarea#stepcodeTextarea1151").val(newCode); if(alreadyWriteCode1151!=code1151){ lastModifedTime1151 = new Date().getTime(); $("#rendering1151").show(); $("#rendered1151").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1151 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1151.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1151").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

时:分:秒:毫秒

分别获取时:分:秒:毫秒
"); window.frames["iframe_show1152"].document.write(decodeHtml($("textarea#stepcodeTextarea1152").val())); window.frames["iframe_show1152"].document.close(); $(window.frames["iframe_show1152"]).load(function(){ $("#iframe_show1152").height($("#iframe_show1152").contents().find("body").height()+showittryitheight); }); $("#iframe_show1152").height($("#iframe_show1152").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var d = new Date(); document.write("分别获取时:分:秒:毫秒 "); document.write(d.getHours()); document.write(":"); document.write(d.getMinutes()); document.write(":"); document.write(d.getSeconds()); document.write(":"); document.write(d.getMilliseconds()); </script>
<script>
  var d = new Date();
  document.write("分别获取时:分:秒:毫秒 ");
  document.write(d.getHours());
  document.write(":");
  document.write(d.getMinutes());
  document.write(":");
  document.write(d.getSeconds());
  document.write(":");
  document.write(d.getMilliseconds());
</script>
"); window.frames["iframe1152"].document.write(decodeHtml(code1152)); window.frames["iframe1152"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1152"]).load(function(){ $("#iframe1152").height($("#iframe1152").contents().find("body").height()+showittryitheight); }); $("#iframe1152").height($("#iframe1152").contents().find("body").height()+showittryitheight); alreadyWriteCode1152 = code1152; $("#rendering1152").hide(); $("#rendered1152").show(); } var tRereshRetry2DemoPanel1152 = setInterval(rereshRetry2DemoPanel1152,1000); var binded1152 = false; $("textarea#stepcodeTextarea1152").keyup(function(){ if(!binded1152){ $(window).bind('beforeunload',function(){ binded1152 = true; return "xxxx"; }); } var newCode = $(this).val() code1152 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1152!=newCode){ // window.frames["iframe1152"].document.write("
"); // window.frames["iframe1152"].document.write(decodeHtml($("textarea#stepcodeTextarea1152").val())); // window.frames["iframe1152"].document.close(); // $(window.frames["iframe1152"]).load(function(){ // $("#iframe1152").height($("#iframe1152").contents().find("body").height()+showittryitheight); // }); // code1152 = newCode; // } }); $(".tryButton1152").click(function(){ $("#tryDiv1152").show(); $("#stepcodeTextarea1152").focus(); $("#stepcodeTextarea1152").height(200); $("#iframe1152").height(0); window.frames["iframe1152"].document.write("
"); window.frames["iframe1152"].document.write(decodeHtml($("textarea#stepcodeTextarea1152").val())); window.frames["iframe1152"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1152"]).load(function(){ $("#iframe1152").height($("#iframe1152").contents().find("body").height()+showittryitheight); }); $("#iframe1152").height($("#iframe1152").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1152.focus(); editor1152.setSize(null, "250"); $("#rendering1152").hide(); $("#rendered1152").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1152 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1152"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1152.on("change",function(doc){ if(!binded1152){ $(window).bind('beforeunload',function(){ binded1152 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1152 = newCode; $("textarea#stepcodeTextarea1152").val(newCode); if(alreadyWriteCode1152!=code1152){ lastModifedTime1152 = new Date().getTime(); $("#rendering1152").show(); $("#rendered1152").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1152 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1152.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1152").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 4 :

一周的第几天

通过getDay()获取,今天是本周的第几天
与getMonth()一样,返回值是基0的。
"); window.frames["iframe_show1153"].document.write(decodeHtml($("textarea#stepcodeTextarea1153").val())); window.frames["iframe_show1153"].document.close(); $(window.frames["iframe_show1153"]).load(function(){ $("#iframe_show1153").height($("#iframe_show1153").contents().find("body").height()+showittryitheight); }); $("#iframe_show1153").height($("#iframe_show1153").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var day=new Date().getDay(); //通过日期对象获取数字形式的星期几 var weeks = new Array("星期天","星期一","星期二","星期三","星期四","星期五","星期六"); document.write("今天是 : "+weeks[day]); </script>
<script>
var day=new Date().getDay(); //通过日期对象获取数字形式的星期几
var weeks = new Array("星期天","星期一","星期二","星期三","星期四","星期五","星期六");

document.write("今天是 : "+weeks[day]);
 
</script>
"); window.frames["iframe1153"].document.write(decodeHtml(code1153)); window.frames["iframe1153"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1153"]).load(function(){ $("#iframe1153").height($("#iframe1153").contents().find("body").height()+showittryitheight); }); $("#iframe1153").height($("#iframe1153").contents().find("body").height()+showittryitheight); alreadyWriteCode1153 = code1153; $("#rendering1153").hide(); $("#rendered1153").show(); } var tRereshRetry2DemoPanel1153 = setInterval(rereshRetry2DemoPanel1153,1000); var binded1153 = false; $("textarea#stepcodeTextarea1153").keyup(function(){ if(!binded1153){ $(window).bind('beforeunload',function(){ binded1153 = true; return "xxxx"; }); } var newCode = $(this).val() code1153 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1153!=newCode){ // window.frames["iframe1153"].document.write("
"); // window.frames["iframe1153"].document.write(decodeHtml($("textarea#stepcodeTextarea1153").val())); // window.frames["iframe1153"].document.close(); // $(window.frames["iframe1153"]).load(function(){ // $("#iframe1153").height($("#iframe1153").contents().find("body").height()+showittryitheight); // }); // code1153 = newCode; // } }); $(".tryButton1153").click(function(){ $("#tryDiv1153").show(); $("#stepcodeTextarea1153").focus(); $("#stepcodeTextarea1153").height(200); $("#iframe1153").height(0); window.frames["iframe1153"].document.write("
"); window.frames["iframe1153"].document.write(decodeHtml($("textarea#stepcodeTextarea1153").val())); window.frames["iframe1153"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1153"]).load(function(){ $("#iframe1153").height($("#iframe1153").contents().find("body").height()+showittryitheight); }); $("#iframe1153").height($("#iframe1153").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1153.focus(); editor1153.setSize(null, "250"); $("#rendering1153").hide(); $("#rendered1153").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1153 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1153"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1153.on("change",function(doc){ if(!binded1153){ $(window).bind('beforeunload',function(){ binded1153 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1153 = newCode; $("textarea#stepcodeTextarea1153").val(newCode); if(alreadyWriteCode1153!=code1153){ lastModifedTime1153 = new Date().getTime(); $("#rendering1153").show(); $("#rendered1153").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1153 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1153.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1153").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 5 :

经历的毫秒数

获取从1970/1/1 08:00:00 至今的毫秒数
"); window.frames["iframe_show1154"].document.write(decodeHtml($("textarea#stepcodeTextarea1154").val())); window.frames["iframe_show1154"].document.close(); $(window.frames["iframe_show1154"]).load(function(){ $("#iframe_show1154").height($("#iframe_show1154").contents().find("body").height()+showittryitheight); }); $("#iframe_show1154").height($("#iframe_show1154").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var time = new Date().getTime(); document.write("从1970/1/1 08:00:00 到今天的毫秒数: "+ time); </script>
<script>
var time = new Date().getTime();
document.write("从1970/1/1 08:00:00 到今天的毫秒数: "+ time);
</script>
"); window.frames["iframe1154"].document.write(decodeHtml(code1154)); window.frames["iframe1154"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1154"]).load(function(){ $("#iframe1154").height($("#iframe1154").contents().find("body").height()+showittryitheight); }); $("#iframe1154").height($("#iframe1154").contents().find("body").height()+showittryitheight); alreadyWriteCode1154 = code1154; $("#rendering1154").hide(); $("#rendered1154").show(); } var tRereshRetry2DemoPanel1154 = setInterval(rereshRetry2DemoPanel1154,1000); var binded1154 = false; $("textarea#stepcodeTextarea1154").keyup(function(){ if(!binded1154){ $(window).bind('beforeunload',function(){ binded1154 = true; return "xxxx"; }); } var newCode = $(this).val() code1154 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1154!=newCode){ // window.frames["iframe1154"].document.write("
"); // window.frames["iframe1154"].document.write(decodeHtml($("textarea#stepcodeTextarea1154").val())); // window.frames["iframe1154"].document.close(); // $(window.frames["iframe1154"]).load(function(){ // $("#iframe1154").height($("#iframe1154").contents().find("body").height()+showittryitheight); // }); // code1154 = newCode; // } }); $(".tryButton1154").click(function(){ $("#tryDiv1154").show(); $("#stepcodeTextarea1154").focus(); $("#stepcodeTextarea1154").height(200); $("#iframe1154").height(0); window.frames["iframe1154"].document.write("
"); window.frames["iframe1154"].document.write(decodeHtml($("textarea#stepcodeTextarea1154").val())); window.frames["iframe1154"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1154"]).load(function(){ $("#iframe1154").height($("#iframe1154").contents().find("body").height()+showittryitheight); }); $("#iframe1154").height($("#iframe1154").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1154.focus(); editor1154.setSize(null, "250"); $("#rendering1154").hide(); $("#rendered1154").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1154 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1154"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1154.on("change",function(doc){ if(!binded1154){ $(window).bind('beforeunload',function(){ binded1154 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1154 = newCode; $("textarea#stepcodeTextarea1154").val(newCode); if(alreadyWriteCode1154!=code1154){ lastModifedTime1154 = new Date().getTime(); $("#rendering1154").show(); $("#rendered1154").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1154 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1154.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1154").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 6 :

修改日期和时间

把日期对象设置为2012/12/12 00:00:00
"); window.frames["iframe_show1155"].document.write(decodeHtml($("textarea#stepcodeTextarea1155").val())); window.frames["iframe_show1155"].document.close(); $(window.frames["iframe_show1155"]).load(function(){ $("#iframe_show1155").height($("#iframe_show1155").contents().find("body").height()+showittryitheight); }); $("#iframe_show1155").height($("#iframe_show1155").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var d=new Date(); document.write("修改日期对象的值为世界末日:<br>"); d.setFullYear(2012); d.setMonth(11); //月份时基0的,所以11表示12月 d.setDate(12); d.setHours(0); d.setMinutes(0); d.setSeconds(0); document.write(d); </script>
<script>
var d=new Date();
document.write("修改日期对象的值为世界末日:<br>");
d.setFullYear(2012);
d.setMonth(11); //月份时基0的,所以11表示12月
d.setDate(12);

d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);

document.write(d);
</script>
"); window.frames["iframe1155"].document.write(decodeHtml(code1155)); window.frames["iframe1155"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1155"]).load(function(){ $("#iframe1155").height($("#iframe1155").contents().find("body").height()+showittryitheight); }); $("#iframe1155").height($("#iframe1155").contents().find("body").height()+showittryitheight); alreadyWriteCode1155 = code1155; $("#rendering1155").hide(); $("#rendered1155").show(); } var tRereshRetry2DemoPanel1155 = setInterval(rereshRetry2DemoPanel1155,1000); var binded1155 = false; $("textarea#stepcodeTextarea1155").keyup(function(){ if(!binded1155){ $(window).bind('beforeunload',function(){ binded1155 = true; return "xxxx"; }); } var newCode = $(this).val() code1155 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1155!=newCode){ // window.frames["iframe1155"].document.write("
"); // window.frames["iframe1155"].document.write(decodeHtml($("textarea#stepcodeTextarea1155").val())); // window.frames["iframe1155"].document.close(); // $(window.frames["iframe1155"]).load(function(){ // $("#iframe1155").height($("#iframe1155").contents().find("body").height()+showittryitheight); // }); // code1155 = newCode; // } }); $(".tryButton1155").click(function(){ $("#tryDiv1155").show(); $("#stepcodeTextarea1155").focus(); $("#stepcodeTextarea1155").height(200); $("#iframe1155").height(0); window.frames["iframe1155"].document.write("
"); window.frames["iframe1155"].document.write(decodeHtml($("textarea#stepcodeTextarea1155").val())); window.frames["iframe1155"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1155"]).load(function(){ $("#iframe1155").height($("#iframe1155").contents().find("body").height()+showittryitheight); }); $("#iframe1155").height($("#iframe1155").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1155.focus(); editor1155.setSize(null, "250"); $("#rendering1155").hide(); $("#rendered1155").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1155 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1155"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1155.on("change",function(doc){ if(!binded1155){ $(window).bind('beforeunload',function(){ binded1155 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1155 = newCode; $("textarea#stepcodeTextarea1155").val(newCode); if(alreadyWriteCode1155!=code1155){ lastModifedTime1155 = new Date().getTime(); $("#rendering1155").show(); $("#rendered1155").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1155 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1155.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1155").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 7 :

练习-计算年龄

练习难度
Or  姿势不对,事倍功半! 点击查看做练习的正确姿势
选择出生日期,计算年龄
练习-计算年龄
示例 8 :

答案-计算年龄

在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JavaScript-日期 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图