js 怎么获取年月日时分秒中的时分秒

JavaScript016

js 怎么获取年月日时分秒中的时分秒,第1张

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<script>标签,输入js代码:

var a = new Date()document.body.innerHTML

= '时:' + a.getHours() + '<br/>分:' + a.getMinutes() + '<br/>秒:' + a.getSeconds()

3、浏览器运行index.html页面,此时当前时间的时分秒都被js获取并打印了出来。

系统时间一般是值服务端时间,js获取服务端时间的方法是直接用ajax获取。

编写显示时间的页面:

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>Server date/time</title>

<script language="javascript" src="serverDate.js"></script>

</head>

<script language="javascript">

var localTime = new Date()

document.write("Local machine time is: " + localTime + "<br>")

document.write("Server time is: " + date)

</script>

<body>

</body>

ajax脚本获取server的时间

var xmlHttp

function srvTime(){

try {

//创建xmlHttp对象

xmlHttp = new XMLHttpRequest()

}

catch (err1) {

//ie浏览器

try {

xmlHttp = new ActiveXObject('Msxml2.XMLHTTP')

}

catch (err2) {

try {

xmlHttp = new ActiveXObject('Microsoft.XMLHTTP')

}

catch (eerr3) {

//ajax不支持

alert("AJAX not supported")

}

}

}

//打开xmlHttp请求

xmlHttp.open('HEAD',window.location.href.toString(),false)

//设置xmlHttp请求头

xmlHttp.setRequestHeader("Content-Type", "text/html")

//发送请求

xmlHttp.send('')

// 获取response中的Date参数

return xmlHttp.getResponseHeader("Date")

}

var st = srvTime()//服务器时间赋值给st变量

var date = new Date(st)//转换js的date对象

// 输出服务器时间

document.write("服务器时间: " + date)

拓展资料:

JavaScript一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标准通用标记语言下的一个应用)网页上使用,用来给HTML网页增加动态功能。

语句:JavaScript程序是由若干语句组成的,语句是编写程序的指令。JavaScript提供了完整的基本编程语句,

它们是:赋值语句、switch选择语句、while循环语句、for循环语句、for each循环语句、do...while循环语句、break循环中止语句、continue循环中断语句、with语句、try…catch语句、if语句(if..else,if…else if…)。

<script language="Javascript">

<!--

function show(){

if (!document.layers&&!document.all)

return

var Digital=new Date()

var todayyear=Digital.getYear()

var todaymonth=Digital.getMonth()

var todaydate=Digital.getDate()

var todayday=Digital.getDay()

var hours=Digital.getHours()

var minutes=Digital.getMinutes()

var seconds=Digital.getSeconds()

var swatchseconds=hours*3600+minutes*60+seconds

var zoneseconds=7*3600

var beat=((swatchseconds-zoneseconds)*10-(swatchseconds-zoneseconds)*10%864)/864

if (beat <0) beat = 1000+beat

if (beat <100) beat = "0"+beat

if (beat <10) beat = "0"+beat

if (todayday == 0) todayday = "<font class=time color=red>星期天</font>"

if (todayday == 1) todayday = "<font class=time color=blue>星期一</font>"

if (todayday == 2) todayday = "<font class=time color=blue>星期二</font>"

if (todayday == 3) todayday = "<font class=time color=blue>星期三</font>"

if (todayday == 4) todayday = "<font class=time color=blue>星期四</font>"

if (todayday == 5) todayday = "<font class=time color=blue>星期五</font>"

if (todayday == 6) todayday = "<font class=time color=red>星期六</font>"

if (hours<=9) hours="0"+hours

if (minutes<=9) minutes="0"+minutes

if (seconds<=9) seconds="0"+seconds

myclock="<font color=blue>"+todayyear+"年"+(todaymonth+1)+"月"+todaydate+"日</font>"+todayday+" <font color=blue>"+hours+":"+minutes+":"+seconds+" <font class=time color=red></font>"

if (document.layers){

document.layers.clock.document.write(myclock)

document.layers.clock.document.close()

}

else if (document.all)

clock.innerHTML=myclock

setTimeout("show()",1000)

}

//-->

</script>

<body onload=show()>

<div><span class="time" id="clock" name="clock"></span></div>

</body>