欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

JS简单获取及显示当前时间的方法

时间:2021-04-15 11:16:11|栏目:JavaScript代码|点击:

本文实例讲述了JS简单获取及显示当前时间的方法。分享给大家供大家参考,具体如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>当前时间</title>
</head>
<body>
 <script>
var now = new Date();
var year = now.getFullYear();    //年
var month = now.getMonth() + 1;   //月
var day = now.getDate();      //日
var hh = now.getHours();      //时
var mm = now.getMinutes();     //分
var clock = year + "-";
if (month < 10)
clock += "0";
clock += month + "-";
if (day < 10)
clock += "0";
clock += day + " ";
if (hh < 10)
clock += "0";
clock += hh + ":";
if (mm < 10) clock += '0';
clock += mm;
document.write(clock);
</script>
</body>
</html>

PS:对JavaScript时间与日期操作感兴趣的朋友还可以参考本站在线工具:

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

在线世界各地时间查询:
http://tools.jb51.net/zhuanhuanqi/worldtime

在线万年历日历:
http://tools.jb51.net/bianmin/wannianli

网页万年历日历:
http://tools.jb51.net/bianmin/webwannianli

更多关于JavaScript相关内容可查看本站专题:《JavaScript时间与日期操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结

希望本文所述对大家JavaScript程序设计有所帮助。

上一篇:javascript 输入文本框时的友好提示

栏    目:JavaScript代码

下一篇:javascript使用正则表达式检测IP地址

本文标题:JS简单获取及显示当前时间的方法

本文地址:http://www.codeinn.net/misctech/101492.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有