Date 对象
// var date1 = new Date()
// console.log(date1);
// 创建Date实例用来处理日期和时间。
// Date 对象基于1970年1月1日(世界标准时间)起的毫秒数。
// console.log(typeof date1);
// 检查一下是什么类型
// console.dir(date1);
// 也是一样的
var date1 = new Date()
console.log("年份"+date1.getFullYear()+1);
// 年份2021
console.log("月份"+date1.getMonth());
// 打印出来的是十月,但是今年是十一月 所以 要加一。
// 规定是0-11月(也是十二个,只是从零开始)
console.log("日期"+date1.getDate());
// 日期是24
console.log("时"+date1.getHours());
// 这个是小时
console.log("分"+date1.getMinutes());
// 这个是分钟
console.log("秒"+date1.getSeconds());
// 这个是秒
console.log("毫秒"+date1.getMilliseconds());
// 这个是毫秒
setInterval(function(){
var date1 = new Date()
console.log(date1.getHours()+":"+date1.getMinutes()+":"+
date1.getSeconds());
},1000)
// 计算时间的方法 但是他在只有个位数的时候是1 2 3
// 并不是我们所谓的01 02 03 所以不太准确
// 以下是给这个1 2 3 变为01 02 03
function getTime(){
var a = new Date()
// 方法1: ifelse
if(a.getHours()<10){
var h = "0"+a.getHours()
}else {
var h = a.getHours()
}
// 方法2: 三木表达式 也就是
// 表达式 ? 表达式 : 表达式
var h = a.getHours()<10?"0"+a.getHours:a.getHours()
// 小时 意思是 如果这个数小于10,那么这个数前面加0,>10不加
var m = a.getMinutes()<10?"0"+a.getMinutes():a.getMinutes()
// 分钟
var s = a.getSeconds()<10?"0"+a.getSeconds():a.getSeconds()
// 方法3: 封装函数
var h = add0(a.getHours())
var m = add0(a.getMinutes())
var s = add0(a.getSeconds())
return h +":"+m+":"+s
}
function add0(num){
if(num<10){
return"0"+num
}else{
return num
}
}
setInterval(function(){
console.log(getTime());
},1000)

浙公网安备 33010602011771号