javaScript的Date函数1、获取当前时间
  Date()获取到的时间是当前设备的显⽰的时间,开发中要考虑到⽤户的设备时间是否正确let nowTime = new Date(); // 获取当前时间
  把data时间转换成常规格式
<script>
let getTimeNow = () => {
let nowTime = new Date(); // 获取当前时间——Sun Jan 12 2020 14:07:22 GMT+0800 (中国标准时间)                let year = FullYear(); // 年
let month = Month()+1; // ⽉这⾥要加1
let day = Date(); // 获取⼏号
let hour = Hours();//得到⼩时
let minu = Minutes();//得到分钟
let sec = Seconds();//得到秒
let week = Day();//得到星期⼏
let timeStr = '';
timeStr += year + '/' + isGreaterthanTen(month) + '/' + isGreaterthanTen(day);
timeStr += '-' + isGreaterthanTen(hour) + '/' + isGreaterthanTen(minu) + '/' + isGreaterthanTen(sec);
timeStr += '-' + getWeek(week);
return timeStr;
};
let isGreaterthanTen = num => {
if (parseInt(num, 10) >= 10) {
return num;
} else {
return '0' + num;
}
};
let getWeek = week => {
if (parseInt(week, 10) === 0) {
return '星期⽇'
} else if (parseInt(week, 10) === 1) {
return '星期⼀'
} else if (parseInt(week, 10) === 2) {
return '星期⼆'
} else if (parseInt(week, 10) === 3) {
return '星期三'
} else if (parseInt(week, 10) === 4) {
return '星期四'
} else if (parseInt(week, 10) === 5) {
return '星期五'
} else {
return '星期六'
}
};
console.log(getTimeNow()); // 2020/01/12-14/25/29-星期⽇
</script>
2、时间戳
  获取当前时间戳:
       // 获取当前时间戳
let timestamp1 = Date.parse(new Date()); // 精确到秒
let timestamp2 = (new Date()).valueOf(); // 精确到毫秒
let timestamp3 = new Date().getTime(); // 精确到毫秒
let timestamp4 = Number(new Date()) ; // 精确到毫秒
let timestamp5 = w(); // 精确到毫秒
  把时间转换成时间戳
<script>
let getTimeNow = (nowTime) => {
// let nowTime = new Date(); // 获取当前时间——Sun Jan 12 2020 14:07:22 GMT+0800 (中国标准时间)                let year = FullYear(); // 年
let month = Month()+1; // ⽉这⾥要加1
let day = Date(); // 获取⼏号
let hour = Hours();//得到⼩时
let minu = Minutes();//得到分钟
let sec = Seconds();//得到秒
let week = Day();//得到星期⼏
let timeStr = '';
timeStr += year + '/' + isGreaterthanTen(month) + '/' + isGreaterthanTen(day);
timeStr += '-' + isGreaterthanTen(hour) + '/' + isGreaterthanTen(minu) + '/' + isGreaterthanTen(sec);
timeStr += '-' + getWeek(week);
return timeStr;
};
let isGreaterthanTen = num => {
if (parseInt(num, 10) >= 10) {
return num;
} else {
return '0' + num;
}
};
let getWeek = week => {
if (parseInt(week, 10) === 0) {
return '星期⽇'
} else if (parseInt(week, 10) === 1) {
return '星期⼀'
} else if (parseInt(week, 10) === 2) {
return '星期⼆'
} else if (parseInt(week, 10) === 3) {
return '星期三'
javascript 函数
} else if (parseInt(week, 10) === 4) {
return '星期四'
} else if (parseInt(week, 10) === 5) {
return '星期五'
} else {
return '星期六'
}
};
// 获取当前时间戳
let timestamp1 = Date.parse(new Date()); // 精确到秒
let timestamp2 = (new Date()).valueOf(); // 精确到毫秒
let timestamp3 = new Date().getTime(); // 精确到毫秒
let timestamp4 = Number(new Date()) ; // 精确到毫秒
let timestamp5 = w(); // 精确到毫秒
let timeStr = new Date(1578811134211); // 放⼊时间戳
console.log(getTimeNow(timeStr)); // 2020/01/12-14/38/54-星期⽇
console.LocaleDateString().replace(/\//g, "-") + " " + TimeString().substr(0, 8)); //再利⽤拼接正则等⼿段转化为yyyy-MM-dd hh:mm:ss 格式        </script>
3、倒计时
  计算时间戳
<script>
let timeEnd = 1578987896323; // 结束时间的时间戳
let countDown = (timeEnd) => {
let timeNow = new Date().getTime(); // 当前时间戳
let remainingTime = parseInt(timeEnd) - parseInt(timeNow);
secondTime(remainingTime);
};
let isGreaterthanTen = num => {
if (parseInt(num, 10) >= 10) {
return num;
} else {
return '0' + num;
}
};
let secondTime = (remainingTime) => {
/*
1 秒 s = 1000 毫秒 ms
1 分 min = 60 秒 s
1 时 hour = 60分 min
1 天 day = 24 ⼩时 hour
1天等于 86400000毫秒(ms)
* */
let timeStr = '';
let days = Math.floor(remainingTime/(24*3600*1000)); // 相差天数
if (days > 0) {
timeStr += isGreaterthanTen(days) + '天';
} else {
timeStr += '00天'
}
let hoursLeave = remainingTime%(24*3600*1000)    //计算天数后剩余的毫秒数
let hours = Math.floor(hoursLeave/(3600*1000));  // 相差⼩时数
if (hours > 0) {
timeStr += isGreaterthanTen(hours) + '时';
} else {
timeStr += '00时';
}
let minLeave = hoursLeave%(3600*1000);        //计算⼩时数后剩余的毫秒数                let minutes = Math.floor(minLeave/(60*1000));
if (minutes > 0) {
timeStr += isGreaterthanTen(minutes) + '分';
} else {
timeStr += '00分';
}
let secounLeave =minLeave%(60*1000);      //计算分钟数后剩余的毫秒数                let und(secounLeave/1000);
if (seconds > 0) {
timeStr += isGreaterthanTen(seconds) + '秒'
} else {
timeStr += '00秒';
}
return timeStr;
}
console.log(countDown(timeEnd));
</script>
  4、yyyy-MM-dd HH:mm:ss 转换成时间戳
// 获取某个时间格式的时间戳
let stringTime = "2020-01-01 00:00:00";
let timestamp = (new Date(stringTime)).getTime();
console.log(timestamp);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。