时间戳转formdate格式 13 || 16 位
function timeStampToFormdate(timestamp) {
if (!timestamp) {
return ''
}
var date = timestamp.toString().length == "13" ? new Date(timestamp) : new Date(timestamp * 1000); //时间戳为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;
}
console.log(timeStampToFormdate(new Date().getTime()));
获取距离当前时间多少天前的时间
function manyDayBeforeNowDay(days) {
if (days * 1) {
return timeStampToFormdate(new Date().getTime() - days * 60 * 60 * 24 * 1000)
} else {
return ''
}
}
console.log(manyDayBeforeNowDay(2));
时间格式化返回年月日时分秒 格式自己拼
/**
*如果没有传入时间,就返回当前时间的格式化数据
*/
function dateFormat(date) {
!date && date=new Date();
if (date) {
let newDate = new Date(date);
let n = newDate.getFullYear();
let y = (newDate.getMonth() + 1 < 10 ? '0' + (newDate.getMonth() + 1) : newDate.getMonth() + 1);
let r = (newDate.getDate() < 10 ? '0' + newDate.getDate() : newDate.getDate());
let s = (newDate.getHours() < 10 ? '0' + newDate.getHours() : newDate.getHours());
let f = (newDate.getMinutes() < 10 ? '0' + newDate.getMinutes() : newDate.getMinutes());
let m = (newDate.getSeconds() < 10 ? '0' + newDate.getSeconds() : newDate.getSeconds());
let format = {
n: n,
y: y,
r: r,
s: s,
f: f,
m: m,
}
return format
}
}
获取当前星期几
function getWeekDate() {
var now = new Date();
var day = now.getDay();
var weeks = new Array(
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六"
);
var week = weeks[day];
return week;
}
根据当前年的第几天 获取所在的日期
/**
* @param {Number} year 哪年
* @param {Number} day 哪天
*/
function dayToMD(year, day) {
if (year && day && day < 367) {
var isrunnian = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
var rules = [31, isrunnian ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
for (var i = 0; i < rules.length; i++) {
if (rules[i] >= day) {
return (i + 1 < 10 ? ("0" + (i + 1)) : (i + 1)) + '-' + (day < 10 ? ("0" + day) : day);
}
day -= rules[i];
}
}
}
获取所在周
记录一下 ,计算规则比较怪。产品这么要求,╮(╯▽╰)╭
/**
* 获取当前天所在的周
* 规则:按照日历上一行为一周。以当前年第一个周一所在的日期为当前年的起点
* 如果当前日期>=起点,计算公式:Math.ceil((当前天数-(起点-1))/7)
* 如果当前日期<起点 ,计算公式:Math.ceil((上一年的天数-(上一年起点-1)+当前日期天数)/7)
* 返回结果 2020-1 2020年第几周
* 头秃
*/
function weekFilter() {
function yearDays(leapY) {
var strleapYis =
(leapY % 4 == 0 && leapY % 100 != 0) || leapY % 400 == 0;
if (strleapYis) {
strleapYis = 366;
} else {
strleapYis = 365;
}
return strleapYis;
}
function getWeekDay(year) {
var d = new Date();
d.setFullYear(year, 0);
for (var i = 1; i <= 31; i++) {
d.setDate(i);
if (d.getDay() == 1) break;
}
d.setFullYear(year, 0, i);
// return year + "-" + "01-" + (i >= 10 ? i :( "0" + i));
return i;
}
let nowDay = Math.ceil(
(new Date() - new Date(new Date().getFullYear().toString())) /
(24 * 60 * 60 * 1000)
);
let nowYear = new Date().getFullYear();
let nowWeek;
if (nowDay >= getWeekDay(nowYear)) {
nowWeek = Math.ceil((nowDay - (getWeekDay(nowYear) - 1)) / 7);
return `${nowYear}-${nowWeek}`
} else {
nowWeek = Math.ceil(
(yearDays(nowYear - 1) - (getWeekDay(nowYear - 1) - 1) + nowDay) / 7
);
return `${nowYear - 1}-${nowWeek}`
}
}