1. 时间表示方式
1)标准Date:Mon Nov 11 2010 10:25:12 GMT+0800
2)时间戳:1672047728525
3)字符串:2012-01-11 10:25:12
2. 时间转换及常见方法
// 1. 获取当前时间戳
new Date().getTime()
Date.now()
// 2. 字符串转标准时间
new Date("2012-01-11 10:25:12")
// 3. 字符串转时间戳
new Date("2012-01-11 10:25:12").getTime()
// 4. 时间戳转字符串
function timestampToTime(timestamp) { // timestamp为number类型
var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
var Y = date.getFullYear() + '.';
var M = ((date.getMonth() + 1) < 10) ? ('0' + (date.getMonth() + 1) + '.') : ((date.getMonth() + 1) + '.');
var D = (date.getDate() < 10) ? ('0' + date.getDate() + ' ') : (date.getDate() + ' ');
var h = (date.getHours() < 10) ? ('0' + date.getHours() + ':') : (date.getHours() + ':');
var m = (date.getMinutes() < 10) ? ('0' + date.getMinutes() + ':') : (date.getMinutes() + ':');
var s = (date.getSeconds() < 10) ? ('0' + date.getSeconds()) : (date.getSeconds());
return Y + M + D + h + m + s;
}
// 5. 时间戳转标准时间
new Date(timestamp)
// 6. 标准时间转时间戳
Date.parse(new Date()) //不推荐使用,不准确
new Date().getTime()
(new Date()).valueOf()
// 5. 标准时间转字符串
function getFormatDate (date) {
let dateNow = new Date()
if (date) {
dateNow = date
}
const seperator1 = '-', seperator2 = ':'
let month = dateNow.getMonth() + 1
month = two_chars(month)
let strDate = dateNow.getDate()
strDate = two_chars(strDate)
let hours = dateNow.getHours()
hours = two_chars(hours)
let minute = dateNow.getMinutes()
minute = two_chars(minute)
let secs = dateNow.getSeconds()
secs = two_chars(secs)
const currentDate = dateNow.getFullYear() + seperator1 + month + seperator1 + strDate + ' ' + hours + seperator2 + minute + seperator2 + secs;
return currentDate
}
function two_chars(count) {
if (count >= 1 && count <= 9) {
count = '0' + count
}
return count
}
//7. 获取时间范围,可得到字符串数组
function getDateRange(num) {
const start = new Date()
const end = new Date()
start.setTime(start.getTime() - 3600 * 1000 * 24 * num);
let a = this.formatDateOrTime(start, '-', true).slice(0, 10)
let b = this.formatDateOrTime(end, '-', true).slice(0, 10)
return [a + ' 00:00:00', b + ' 23:59:59']
}
function formatDateOrTime(date, space, isHasTime) {
const year = date.getFullYear()
const month = (date.getMonth() + 1) < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1
const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate()
const hour = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours()
const minutes = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes()
const seconds = date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds()
let format = ''
if (!isHasTime) {
format = year + space + month + space + day
} else {
format = year + space + month + space + day + ' ' + hour + ':' + minutes + ':' + seconds
}
return format
}
3. 截取URL参数
GetQueryString(name) {
if (window.location.hash.split("?").length <= 1) {
return null;
}
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.hash.split("?")[1].match(reg);
if (r != null) return unescape(r[2]);
return null;
}
4. 常见正则表达式
// 手机格式判断
function mobile(value) {
return /^1[23456789]\d{9}$/.test(value)
}
// 电子邮箱
function email(value) {
return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value)
}
// 身份证号
function idCard(value) {
return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(
value
)
}