js获取当前时间(格式化)- 代码篇
两种方式:
Vue.prototype.method1 = function (){
var currentTime = new Date();
var YY,MM,DD,hh,ii,ss;
YY = new Date().getFullYear();
MM = new Date().getMonth();
DD = new Date().getDate();
hh = new Date().getHours();
ii = new Date().getMinutes();
ss = new Date().getMilliseconds();
console.log(YY+'-'+MM+'-'+DD+' '+hh+':'+ii+':'+ss);
};
Vue.prototype.method2 = function (){
var currentTime = new Date();
var YY,MM,DD,hh,ii,ss;
YY = currentTime.getFullYear();
MM = currentTime.getMonth();
DD = currentTime.getDate();
hh = currentTime.getHours();
ii = currentTime.getMinutes();
ss = currentTime.getMilliseconds();
console.log(YY+'-'+MM+'-'+DD+' '+hh+':'+ii+':'+ss);
};
运行结果:
上述的打印结果,仅供参考;
建议:思考一个问题:
两种方法的不同之处和相同之处?
函数调用一览表:
对应函数 | 取值含义 | 变量定义 |
---|---|---|
new Date() | ||
.getFullYear() | 年份 | YY |
.getMonth() | 月份 | MM |
.getDate() | 日 | DD |
.getHours() | 小时 | hh |
.getMinutes() | 分钟 | ii |
.getSeconds() | 秒 | ss |
以上就是关于“ js时间对象:获取当前时间(格式化)- 代码篇 ” 的全部内容。