我们有时候会需要打印出当前的时间,比如发票上的时间。JS自带了一些函数可以帮我们解决这个问题。
比如:
使用 Date() 方法获得当日的日期:
document.write(Date())
输出结果:
Tue Aug 19 2014 23:29:39 GMT-0400 (AST)
Date 对象用于处理日期和时间。
可以通过 new 关键词来定义 Date 对象。以下代码定义了名为 myDate 的 Date 对象:
var myDate=new Date()
注释:Date 对象自动使用当前的日期和时间作为其初始值。
返回当前时间:
var dateDigitToString = function (num) {
return num < 10 ? '0' + num : num;
}; //这个函数的作用是把个位数的十位置0,凑成两位数,如:08 04
var currentDate = new Date(),
year = dateDigitToString(currentDate.getFullYear()),
month = dateDigitToString(currentDate.getMonth() + 1),//Date.getMonth()的返回值是0-11,所以要+1
date = dateDigitToString(currentDate.getDate()),
hour = dateDigitToString(currentDate.getHours()),
minute = dateDigitToString(currentDate.getMinutes()),
second = dateDigitToString(currentDate.getSeconds()),
formattedDateString = year + '年' + month + '月' + date + '日 ' + hour + ':' + minute + ':' + second;
document.write(formattedDateString);
输出结果:
2014年08月19日 23:30:36
同时,还可以转化不同的时间表示方式:
document.write(formattedDateString);
document.write("<br /><br />")
document.write(date + "." + month + "." + year)
document.write("<br /><br />")
document.write(year + "/" + month + "/" + date)
输出结果是:
2014年08月19日 23:35:50
19.08.2014
2014/08/19
分享到:
相关推荐
当前时间 系统时间 js获得系统时间 当前时间 系统时间 js获得系统时间 当前时间 系统时间 js获得系统时间
js获取当前时间yyyymmddhhmmss js获取当前时间yyyymmddhhmmss js获取当前时间yyyymmddhhmmss js获取当前时间yyyymmddhhmmss js获取当前时间yyyymmddhhmmss js获取当前时间yyyymmddhhmmss js获取当前时间...
js动态显示系统当前时间,客户端机器的。
网页特效|Linkweb.cn/Js|---最简单的全日制时间显示,代码非常简单 ; charset=gb2312"> </head> <script>setInterval("linkweb.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new ...
JavaScript 实现打印当前时间
使用javascript自定义方法来显示系统当前的时间
兼容所有浏览器的js,用来动态显示当前时间,只需要调用,并传入显示区域的id即可
js判断当前时间是哪个时间段(早,中,晚)
本文实例讲述了JavaScript将当前时间转换成UTC标准时间的方法。分享给大家供大家参考。具体如下: 这里使用JavaScript将当前时间转换成UTC标准时间,北京在东八区,在北京时间基础上减掉8小时 <!DOCTYPE html> &...
非常好用的JavaScript日期时间空间
js获取页面上的时间和当前时间进行比较判断,js获取页面上的时间和当前时间进行比较判断,js获取页面上的时间和当前时间进行比较判断
js+html获取系统当前时间.docx
使用javascript显示当前时间
js获取当前时间显示在页面上</title> [removed] [removed]=function(){ //定时器每秒调用一次fnDate() setInterval(function(){ fnDate(); },1000); } //js 获取当前时间 function fnDate(){ var oDiv=...
JavaScript中简易代码获取当前系统时间(年月日小时分钟秒)
JS动态显示当前时间 动态的显示当前系统时间 非常好用
使用js实现的在线日期和时间,判断当前具体时间的不同时刻,上午am,下午pm
js中本人尝试过许多方法对时间前进进行计算,后来慢慢发现,通过New Date类对每日的24:00的进位,可以很简便的计算出给定时间的一小时或一天后的时间;此次资源以整时为例,若精确到分秒,思路相同,可先将分秒部分...