export const newDate = (num) => {
const newDate = new Date();
const newYear = parseInt(newDate.getFullYear() - num);
let month = newDate.getMonth() + 1;
let day = newDate.getDate();
if (parseInt(month) < 10) {
month = "0" + month;
}
if (parseInt(day) < 10) {
day = "0" + day;
}
if (month == "02") {
day = day > 28 ? "28" : day
}
return newYear + "/" + month + "/" + day;
}
export const dateFormat = (date) => {
const year = date.getFullYear();
let month = date.getMonth() + 1;
if (parseInt(month) < 10) {
month = "0" + month;
}
let day = date.getDate();
if (parseInt(day) < 10) {
day = "0" + day;
}
return year + "/" + month + "/" + day;
}
export const dateInterval = ({
max = 60,
min = 0
}, callback) => {
const newDate = new Date();
const maxYear = newDate.getFullYear() - max - 1;
const month = newDate.getMonth() + 1;
const day = newDate.getDate();
const minDate = new Date(maxYear + "/" + month + "/" + day);
let maxDate;
if (min > 0) {
const minYear = newDate.getFullYear() - min;
maxdate = new Date(minYear + "/" + month + "/" + day);
maxDate = new Date(maxdate.getTime() - 24 * 60 * 60 * 1000);
} else {
maxDate = new Date(newDate.getTime() - 30 * 24 * 60 * 60 * 1000)
}
callback && callback({
max: maxDate,
min: new Date(minDate.getTime() + 24 * 60 * 60 * 1000)
})
return {
max: maxDate,
min: new Date(minDate.getTime() + 24 * 60 * 60 * 1000)
}
}
export const getAgeByBirth = (birthday) => {
if (!(/^(\d{4})(\/)(\d{2})(\/)(\d{2})$/.test(birthday))) {
return "";
}
let age;
const year = birthday.substring(0, birthday.indexOf("/")) * 1;
const month = birthday.substring(birthday.indexOf("/") + 1, birthday.lastIndexOf("/")) * 1;
const day = birthday.substring(birthday.lastIndexOf("/") + 1) * 1;
const date = new Date();
const currentYear = date.getFullYear();
const currentMonth = date.getMonth() + 1;
const cuttentDay = date.getDate();
if (month > currentMonth) {
age = currentYear - year - 1;
} else if (month < currentMonth) {
age = currentYear - year;
} else {
if (day > cuttentDay) {
age = currentYear - year - 1;
} else {
age = currentYear - year;
}
}
return age > 0 ? age : 0;
}
export const getBirthByIdNo = (iIdNo) => {
let tmpStr = "";
if (iIdNo.length == 15) {
tmpStr = iIdNo.substring(6, 12);
tmpStr = "19" + tmpStr;
tmpStr = tmpStr.substring(0, 4) + "/" + tmpStr.substring(4, 6) + "/" + tmpStr.substring(6);
} else {
tmpStr = iIdNo.substring(6, 14);
tmpStr = tmpStr.substring(0, 4) + "/" + tmpStr.substring(4, 6) + "/" + tmpStr.substring(6);
}
return tmpStr;
}
export const getQueryUrlParamVal = (name) => {
if (location.href.indexOf("?") == -1 || location.href.indexOf(name + '=') == -1) {
return '';
}
var queryString = location.href.substring(location.href.indexOf("?") + 1);
queryString = decodeURI(queryString);
var parameters = queryString.split("&");
var pos, paraName, paraValue;
for (var i = 0; i < parameters.length; i++) {
pos = parameters[i].indexOf('=');
if (pos == -1) {
continue;
}
paraName = parameters[i].substring(0, pos);
paraValue = parameters[i].substring(pos + 1);
if (paraName == name) {
return unescape(paraValue.replace(/\+/g, " "));
}
}
return '';
}
export const trimNull = (val) => {
if (val && val != "null" && val != "undefind") {
return val;
}
return "";
}
export const maleOrFemalByIdCard = (idCard) => {
idCard = idCard.replace(/\s/g, "");
if (idCard.length == 15) {
if (idCard.substring(14, 15) % 2 == 0) {
return '2';
} else {
return '1';
}
} else if (idCard.length == 18) {
if (idCard.substring(14, 17) % 2 == 0) {
return '2';
} else {
return '1';
}
} else {
return null;
}
}