系统时间获取
var myDate = new Date(); //Thu Sep 24 2020 09:28:05 GMT+0800 (中国标准时间)
myDate.getYear(); //获取当前年份(2位) 120
myDate.getFullYear(); //获取完整的年份 2020
myDate.getMonth(); //获取当前月份(0-11,0代表1月) 8
myDate.getDate(); //获取当前日(1-31) 24
myDate.getDay(); //获取当前星期X(0-6,0代表星期天) 4
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) 1600911278653
myDate.getHours(); //获取当前小时数(0-23) 9
myDate.getMinutes(); //获取当前分钟数(0-59) 35
myDate.getSeconds(); //获取当前秒数(0-59) 50
myDate.getMilliseconds(); //获取当前毫秒数(0-999) 710
myDate.toLocaleDateString(); //获取当前日期 2020/9/24
myDate.toLocaleTimeString(); //获取当前时间 上午9:39:42
myDate.toLocaleString( ); //获取日期与时间 2020/9/24 上午9:40:00
将日期格式2018-09-11 08:00:00 转化为时间戳1536645052000
var t = '2018-09-11 13:50:52';
t = t.replace(/-/g, '/'); // 2018/09/11 13:50:52 由于iOS无法识别2018-09-11 13:50:52'
var time = new Date(t); // Tue Sep 11 2018 13:50:52 GMT+0800 (中国标准时间)
time = time.getTime(); // 1536645052000
倒计时:6天13时22分56秒
<div id="demo"></div>
<script>
var int = setInterval(function() {
let nowTime = new Date();
let endTime = new Date("2020/10/01 00:00:00"); //
const t = endTime - nowTime;
if (t >= 0) {
let day = Math.floor(t / 1000 / 60 / 60 / 24);
let hours = Math.floor(t / 1000 / 60 / 60 % 24)
let minutes = Math.floor(t / 1000 / 60 % 60);
let seconds = Math.floor(t / 1000 % 60);
hours < 10 ? hours = '0' + hours : null;
minutes < 10 ? minutes = '0' + minutes : null;
seconds < 10 ? seconds = '0' + seconds : null;
let str = day + '天' + hours + '时' + minutes + '分' + seconds + '秒'
document.getElementById("demo").innerHTML = str
}
}, 1000)
</script>
Tue Nov 10 2020 00:00:00 GMT+0800 (中国标准时间)
转化
2020-11-10 00:00:00
// 日期转换
transforDateToStr(times) {
var time = times.getTime()
var date = new Date(time)
var year = date.getFullYear() + '-'
var month = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
var current = year + month + day + ' ' + h + ':' + m + ':' + s
return current
},
setTimeout(() => {
}, 0)