JavaScript中如何获得系统当前时间

functiontime(){varnow=newDate();varmonth=now.getMonth();varday=now.getDay();varhour=n... function time(){
var now=new Date();
var month=now.getMonth();
var day=now.getDay();
var hour=now.getHours();
var min=now.getMinutes();
var s=now.getSeconds();
document.mytime.nowtime.value="当前时间:"month+"月"+day+"日"+hour+"时"+min+"分"+s+"秒";
setTimeout("time()",1000);}此代码获取不了month和day,显示为0月0日,难道now Date()方法不能获取年月日?
展开
 我来答
匿名用户
2013-07-16
展开全部
应该是:
var daynow=day.getdate();
document.f1.txt1.value= daynow;]
给你个我用的时间函数,自己修改。
<script language=javascript>
var tTick;
function date() {
var enabled = 0; today = new Date();
var day; var date;
if(today.getDay()==0) day = "星期日"
else if(today.getDay()==1) day = "星期一"
else if(today.getDay()==2) day = "星期二"
else if(today.getDay()==3) day = "星期三"
else if(today.getDay()==4) day = "星期四"
else if(today.getDay()==5) day = "星期五"
else if(today.getDay()==6) day = "星期六";
if (today.getYear()>=2000)
date1 = (today.getYear()) + "年" + (today.getMonth() + 1 ) + "月" + today.getDate() + "日 ";
else
date1 = (1900 + today.getYear()) + "年" + (today.getMonth() + 1 ) + "月" + today.getDate() + "日 ";
return date1+day;
}

function tick() {
var hours, minutes, seconds, ap;
var intHours, intMinutes, intSeconds;
var today;

today = new Date();

intHours = today.getHours();
intMinutes = today.getMinutes();
intSeconds = today.getSeconds();

if (intHours == 0) {
hours = "12:";
ap = "AM";
} else if (intHours < 12) {
hours = intHours+":";
ap = "AM";
} else if (intHours == 12) {
hours = "12:";
ap = "PM";
} else {
intHours = intHours - 12
hours = intHours + ":";
ap = "PM";
}

if (intMinutes < 10) {
minutes = "0"+intMinutes+":";
} else {
minutes = intMinutes+":";
}

if (intSeconds < 10) {
seconds = "0"+intSeconds+" ";
} else {
seconds = intSeconds+" ";
}

timeString = hours+minutes+seconds+ap;

timeString = "<font color='red'>"+"<center>"+date()+" "+timeString+"</center>"+"</font>";
Clock.innerHTML = timeString

tTick=window.setTimeout("tick();", 1000)
}

document.writeln("<div id=Clock> </div>")
window.onload = tick
window.onunload = new Function("clearTimeout(tTick)")
</script>
匿名用户
2013-07-16
展开全部
month这个参数的值是0~11的 所以月份应该是 var month= now.getMonth() + 1; //获取月,即01 now.getDay(); //是获取该天的星期值 几日是now.getDate();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-07-16
展开全部
var day=now.getDay(); //getDay好像是获取星期几..var day=now.getDate();//这个是获取几号的.var month=now.getMonth()+1; //月份需要+1 是从0-11 表示1到12月的.现在1月,所以是0
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友f70e843
2014-05-05
知道答主
回答量:5
采纳率:0%
帮助的人:2.4万
展开全部
Calendar date = Calendar.getInstance();
int day = date.get(Calendar.DAY_OF_MONTH);
int month = date.get(Calendar.MONTH) + 1;
int year = date.get(Calendar.YEAR);
System.out.println("day=" + day);
System.out.println("month=" + month);
System.out.println("year=" + year);
如果再不对的话,看一下你系统的时间对不对。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式