网站首页 > 知识剖析 正文
年月日
function yearToDay(time) {
var y = time.getFullYear(),
m = time.getMonth() + 1,
d = time.getDate();
m = m < 10 ? "0" + m : m;
d = d < 10 ? "0" + d : d;
return y + "-" + m + "-" + d;
};
//2022-03-31
年月日时分秒
function yearToSecond(time) {
var h = time.getHours(),
i = time.getMinutes(),
s = time.getSeconds(),
h = h < 10 ? "0" + h : h;
i = i < 10 ? "0" + i : i;
s = s < 10 ? "0" + s : s;
return yearToDay(time) + ' ' + h + ':' + i + ':' + s;
};
//2022-03-31 10:15:20
年月日,时分秒补0
function yearToZero(){
return yearToDay(time)+ " 00:00:00"
}
//2022-03-31 00:00:00
当前时间的前一天,后一天
let curDate = new Date()
var preDate = new Date(curDate.getTime() - 24*60*60*1000); //前一天
var nextDate = new Date(curDate.getTime() + 24*60*60*1000);//后一天
console.log(yearToSecond(preDate))//前一天
console.log(yearToSecond(nextDate))//后一天
//2022-03-30 10:15:20
//2022-04-01 10:15:20
当前月份第一天和最后一天
function getCurDay(){
let nowdays = new Date(),
year = nowdays.getFullYear(),
month = nowdays.getMonth() + 1;
month = month > 9 ? month : "0" + month;
let firstDayOfCurMonth = `${year}-${month}-01`,
lastDay = new Date(year, month, 0),
lastDayOfCurMonth = `${year}-${month}-${lastDay.getDate()}`;
return [firstDayOfCurMonth,lastDayOfCurMonth]
};
//['2022-03-01','2022-03-31']
当前月份上个月第一天和最后一天
function getPreDay() {
let nowdays = new Date(),
year = nowdays.getFullYear(),
month = nowdays.getMonth();
if (month == 0) {
month = 12;
year = year - 1;
}
month = month > 9 ? month : "0" + month;
let firstDayOfPreMonth = `${year}-${month}-01`,
lastDay = new Date(year, month, 0),
lastDayOfPreMonth = `${year}-${month}-${lastDay.getDate()}`
return [firstDayOfPreMonth,lastDayOfPreMonth]
};
//['2022-02-01', '2022-02-28']
当前时间最近三个月
function getLast3Month() {
var now = new Date(),
year = now.getFullYear(),
month = now.getMonth() + 1,
day = now.getDate(),
dateObj = {};
month = month > 9 ? month : "0" + month;
day = day > 9 ? day : "0" + day;
dateObj.now = year + '-' + month + '-' + day;
if (parseInt(month) == 1) {//如果是1月份,则取上一年的10月份
dateObj.last = (parseInt(year) - 1) + '-10-' + day;
return dateObj;
}
if (parseInt(month) == 2) {//如果是2月份,则取上一年的11月份
dateObj.last = (parseInt(year) - 1) + '-11-' + day;
return dateObj;
}
if (parseInt(month) == 3) {//如果是3月份,则取上一年的12月份
dateObj.last = (parseInt(year) - 1) + '-12-' + day;
return dateObj;
}
var preSize = new Date(year, parseInt(month) - 3, 0).getDate();//开始时间所在月的总天数
if (preSize < parseInt(day)) {
// 开始时间所在月的总天数<本月总天数,比如当前是5月30日,在2月中没有30,则取下个月的第一天(3月1日)为开始时间
let resultMonth = parseInt(month) - 2 < 10 ? ('0' + parseInt(month) - 2) : (parseInt(month) - 2);
dateObj.last = year + '-' + resultMonth + '-01';
return dateObj;
}
if (parseInt(month) <= 10) {
dateObj.last = year + '-0' + (parseInt(month) - 3) + '-' + day;
return dateObj;
} else {
dateObj.last = year + '-' + (parseInt(month) - 3) + '-' + day;
return dateObj;
}
};
//{last: "2021-12-31",now: "2022-03-31" }
没了,结束了,是不是很简单呐,如有问题,欢迎留言。
如果此篇博文对您有帮助,还请动动小手点点关注 点点赞 收藏 留言呐~,谢谢 ~ ~
猜你喜欢
- 2025-05-05 vin码怎么查车型?车辆VIN码的第十位代表什么信息?
- 2025-05-05 Java数组数据的操作之检查日期格式是否正确
- 2025-05-05 苹果序列号怎么看生产日期和产地?
- 2025-05-05 参考文献中的M J N D字母代表什么?
- 2025-05-05 闲鱼交易技巧,满满的干货(闲鱼的交易流程怎么样的?我是买家)
- 2025-05-05 忘记自已多少岁了?可试试年龄计算器
- 2025-05-05 汽车-剖析、解析车架号(VIN)中的第10位-车型年份
- 2025-05-05 干货|史上最全波特酒年份指南(波特酒 年份)
- 2025-05-05 Python版的迷你程序——年月日时分秒
- 2025-05-05 怎么通过VIN查询车型?车架号查车型、配置、年款的方法合集
- 05-05vin码怎么查车型?车辆VIN码的第十位代表什么信息?
- 05-05Java数组数据的操作之检查日期格式是否正确
- 05-05苹果序列号怎么看生产日期和产地?
- 05-05参考文献中的M J N D字母代表什么?
- 05-05闲鱼交易技巧,满满的干货(闲鱼的交易流程怎么样的?我是买家)
- 05-05忘记自已多少岁了?可试试年龄计算器
- 05-05汽车-剖析、解析车架号(VIN)中的第10位-车型年份
- 05-05干货|史上最全波特酒年份指南(波特酒 年份)
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)