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()方法不能获取年月日? 展开
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()方法不能获取年月日? 展开
4个回答
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>
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
如果再不对的话,看一下你系统的时间对不对。
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);
如果再不对的话,看一下你系统的时间对不对。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询