vue date.js
日期方法
// 当前月第一天
export function getDateDay() {
var myDate = new Date();
var tYear = myDate.getFullYear()
var tMonth = myDate.getMonth()
tMonth = doHandleZero(tMonth + 1)
return tYear + "-" + tMonth + "-01"
}
function doHandleZero(zero) {
var date = zero;
if (zero.toString().length == 1) {
date = "0" + zero;
}
return date;
}
// 获取当天时间
export function GetDateStrDay(AddDayCount) {
var dd = new Date()
dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
var y = dd.getFullYear()
var m = dd.getMonth() + 1 // 获取当前月份的日期
var d = dd.getDate()
if (m < 10) {
m = '0' + m
}
if (d < 10) {
d = '0' + d
}
return y + '-' + m + '-' + d
}
// 年月日
export function formatDate_YMD(time) {
if (time === '' || time === undefined || time == null) {
return
}
if (time !== '') {
if (time.toString().substr(6, 13) < 0) {
return ''
} else {
return timeD(new Date(parseInt(time.toString().substr(6, 13))))
}
}
}
function timeD(date) {
var y = date.getFullYear()
var m = date.getMonth() + 1
m = m < 10 ? '0' + m : m
var d = date.getDate()
d = d < 10 ? '0' + d : d
return y + '-' + m + '-' + d
}
// 时分秒
export function formatDate_HMS(time) {
if (time !== '') {
if (time.toString().substr(6, 13) < 0) {
return ''
} else {
return formatTime(new Date(parseInt(time.toString().substr(6, time.length - 2))))
}
}
}
//当前月份
export function getNowMonth() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = month > 9 ? month : "0" + month;
var nowMonth = year + "-" + month;
return nowMonth;
}
//当前年份
export function getNowYear() {
var date = new Date();
var year = date.getFullYear();
return year;
}
// 周一 至 周日
export function formatTime_Week(date) {
const nowTime = date.getTime()
const day = date.getDay() || 7 // 为周日的时候 day 修改为7 否则当天周天会有问题
const oneDayTime = 24 * 60 * 60 * 1000
const MondayTime = nowTime - (day - 1) * oneDayTime // 显示周一
const SundayTime = nowTime + (7 - day) * oneDayTime // 显示周日
return [new Date(MondayTime), new Date(SundayTime)]
}
//根据身份证获取年龄
export function GetAge(identityCard) {
let len = (identityCard + "").length;
let strBirthday = "";
if (len == 18) {
//处理18位的身份证号码从号码中得到生日和性别代码
strBirthday =
identityCard.substr(6, 4) +
"/" +
identityCard.substr(10, 2) +
"/" +
identityCard.substr(12, 2);
}
if (len == 15) {
let birthdayValue = "";
birthdayValue = identityCard.charAt(6) + identityCard.charAt(7);
if (parseInt(birthdayValue) < 10) {
strBirthday =
"20" +
identityCard.substr(6, 2) +
"/" +
identityCard.substr(8, 2) +
"/" +
identityCard.substr(10, 2);
} else {
strBirthday =
"19" +
identityCard.substr(6, 2) +
"/" +
identityCard.substr(8, 2) +
"/" +
identityCard.substr(10, 2);
}
}
let birthDate = new Date(strBirthday);
let nowDateTime = new Date();
let age = nowDateTime.getFullYear() - birthDate.getFullYear();
//再考虑月、天的因素;.getMonth()获取的是从0开始的,这里进行比较,不需要加1
if (
nowDateTime.getMonth() < birthDate.getMonth() ||
(nowDateTime.getMonth() == birthDate.getMonth() &&
nowDateTime.getDate() < birthDate.getDate())
) {
age--;
}
return age;
}
数值
// 数字格式化
// 保留小数点后两位
export function exTwo(val) {
return val
.toString()
.replace(/^(\-)*(\d+)\.(\d{2}).*$/, '$1$2.$3')
.replace(/^\./g, '0.')
}
//只允许写入数字后四位
export function ExData(val) {
return val.toString().replace(/^(\-)*(\d+)\.(\d{4}).*$/, '$1$2.$3')
.replace(/^\./g, '0.')
.replace(/-/g, '')
.replace(/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g, '')
.replace(/[A-Za-z]/g, '')
}
// 保留小数点后四位
export function exFour(val) {
return val
.toString()
.replace(/^(\-)*(\d+)\.(\d{4}).*$/, '$1$2.$3')
.replace(/^\./g, '0.')
}
// 保留小数点后两位, 四舍五入
export function exTwoRound(val) {
return (Math.round(val * 100) / 100).toFixed(2)
}
// 这是对两个数乘法的处理 --- 精度
export function accMul(arg1, arg2) {
var m = 0, s1 = arg1.toString(),
s2 = arg2.toString();
try {
m += s1.split(".")[1].length
} catch (e) { }
try {
m += s2.split(".")[1].length
} catch (e) { }
return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m
)
}
版权声明:本文为weixin_44356010原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。