当前时间的毫秒值
var date = new Date() //得到当前日期对象
var time = date.getTime() //获取13位的毫秒值
console.log(time);
字符串日期转换成毫秒
//支持的格式1
var t1 = '2020-12-20'
var time2 = Date.parse(t1)
console.log(time2);
//支持的格式2
var time3 = Date.parse("Jul 8, 2005")
console.log(time3);
时间拼接
year
(年)
,month
(月)
,hour
(小时)
,minute
(分钟)
,second(
秒
)
var date = new Date()
var year =date.getFullYear()
var month = date.getMonth() + 1
var tian = date.getDate()
console.log(date.getFullYear())
console.log(date.getMonth() + 1)
console.log(date.getDate())
year+"/"+month+"/"+tian //结果:2021/9/27
year+"-"+month+"-"+tian //结果:2021-9-27
定时器
反复执行
1
、启用
setInterval(function() {
console.log(111);
}, 2000)
2
、取消
var timer1 = setInterval(function() {
console.log(111);
}, 2000)
//取消定时器 clearInterval(timer1)
定时只执行一次
1
、启用
var timer1 = setTimeout(function() {
console.log(111);
}, 2000)
2、取消
var timer1 = setTimeout(function() {
console.log(111);
}, 2000)
clearTimeout(timer1)
1.3 String
,
Number
var str = '123' //纯数字字符串 ->转数字
var price = '$25.50' //字符串截取
var price = '25.123123123123' //保留小数位
var date = '2021-09-27' //按照-分割字符串
var num = '11012317823718273' //110***273,保护隐私
String
字符串转数组
//取出年月日,分别打印出来
var date = '2021-09-27' //按照-分割字符串
var arr = date.split('-')
console.log(arr[0]);
console.log(arr[1]);
console.log(arr[2]);