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

JavaScript08

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获取并打印了出来。

<script language="JavaScript">

//日期

var now = new Date()//获取系统日期

var yy = now.getYear()//截取年

var mm = now.getMonth()//截取月

var dd = now.getDay()//截取日

//取时间

var hh = now.getHours()//截取小时

var mm = now.getMinutes()//截取分钟

var ss = now.getTime() % 60000//获取时间,因为系统中时间是以毫秒计算的,

所以秒要通过余60000得到。

ss= (ss - (ss % 1000)) / 1000//然后,将得到的毫秒数再处理成秒

var clock = hh+':'//将得到的各个部分连接成一个日期时间

if (mm <10) clock += '0'//字符串

clock += mm+':'

if (ss <10) clock += '0'

clock += ss

</script>