项目笔记
项目开发笔记
获取当前项目应用上下文(前后端不分离)
function getProjectName(){
var curWwwPath=window.document.location.href;
//获取主机地址之后的目录,如: myproj/view/my.jsp
var pathName=window.document.location.pathname;
var pos=curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8083
var localhostPaht=curWwwPath.substring(0,pos);
//获取带"/"的项目名,如:/myproj
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
//得到了 http://localhost:8083/myproj
var curWwwPath=window.document.location.href;
//获取主机地址之后的目录,如: myproj/view/my.jsp
var pathName=window.document.location.pathname;
var pos=curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8083
var localhostPaht=curWwwPath.substring(0,pos);
//获取带"/"的项目名,如:/myproj
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
return projectName;
}
计算两个日期相差多少时间,工具
转载地址:https://blog.csdn.net/qq_40591925/article/details/87856414
function dayCha(time, twoTime) {
time = time.replace(new RegExp("-", "gm"), "/");
if (twoTime) {
twoTime = twoTime.replace(new RegExp("-", "gm"), "/");
}
else {
twoTime = new Date();
}
// 计算比较日期
const getMaxMinDate = (time, twoTime, type) => {
let minTime = new Date(time).getTime() - new Date(twoTime).getTime() > 0 ? twoTime : time
let maxTime = new Date(time).getTime() - new Date(twoTime).getTime() > 0 ? time : twoTime
let maxDateDay = new Date(new Date(maxTime).getFullYear(), new Date(maxTime).getMonth() + 1, 0).getDate()
let maxMinDate = new Date(minTime).getDate() > maxDateDay ? maxDateDay : new Date(minTime).getDate()
let maxMinTong = null
if (type == 'month') {
maxMinTong = new Date(maxTime).getFullYear() + '/' + (new Date(minTime).getMonth() + 1) + '/' + maxMinDate + ' ' + new Date(minTime).toLocaleTimeString('chinese',{hour12:false})
} else {
maxMinTong = new Date(maxTime).getFullYear() + '/' + (new Date(maxTime).getMonth() + 1) + '/' + maxMinDate + ' ' + new Date(minTime).toLocaleTimeString('chinese',{hour12:false})
}
return {
minTime,
maxTime,
maxMinTong
}
}
// 相差年份
const getYear = (time, twoTime) => {
let oneYear = new Date(time).getFullYear()
let twoYear = new Date(twoTime).getFullYear()
// 计算出与最小日期的日时分相同的最大日期的值
// 如2016-8-17和2019-6-30,计算出来的maxMinTong为2019-8-17
const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, 'month')
// 计算相差多少年
let chaYear = Math.abs(oneYear - twoYear)
// 如果计算出的maxMinTong(如2019-8-17)大于了最大日期,则表示最后一年没有满,需要减一
if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
chaYear--
}
return chaYear
}
const getMonthTong = (time, twoTime) => {
let oneMonth = new Date(time).getFullYear() * 12 + (new Date(time).getMonth() + 1)
let twoMonth = new Date(twoTime).getFullYear() * 12 + (new Date(twoTime).getMonth() + 1)
return Math.abs(oneMonth - twoMonth)
}
// 相差月份
const getMonth = (time, twoTime, value) => {
// 计算出与最小日期的日时分相同的最大日期的值
// 如2016-8-17和2019-6-30,计算出来的maxMinTong为2019-6-17
const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, 'day')
// 计算最小日期到maxMinTong(如2019-6-17)的月份,这样刚好对应月日
let chaMonth = getMonthTong(minTime, maxMinTong)
// 如果计算出来的maxMinTong(如2019-6-17)大于最大日期,则表示最后一月没有满,需要减一
if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
chaMonth--
}
if (value) {
return chaMonth - value
} else {
return chaMonth
}
}
// 相差天数
const getDay = (time, twoTime, value) => {
let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
if (value) {
return parseInt(chaTime / 86400000) - value
} else {
return parseInt(chaTime / 86400000)
}
}
// 相差小时
const getHour = (time, twoTime, value) => {
let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
if (value) {
return parseInt(chaTime / 3600000) - value
} else {
return parseInt(chaTime / 3600000)
}
}
// 相差分钟
const getMinute = (time, twoTime, value) => {
let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
if (value) {
return parseInt(chaTime / 60000) - value
} else {
return parseInt(chaTime / 60000)
}
}
// 相差秒
const getSecond = (time, twoTime, value) => {
let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
if (value) {
return parseInt(chaTime / 1000) - value
} else {
return parseInt(chaTime / 1000)
}
}
// 相差年月日时分秒
const getDffeYMDHMS = (time, twoTime) => {
// 计算出与最小日期的日时分相同的最大日期的值
// 如2016-8-17和2019-6-30,计算出来的maxMinTong为2019-6-17
const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, 'day')
// 计算最小日期到(如:2019-6-17)的天数,这样算出来的天数刚好数足月的
let dffeDay1 = getDay(minTime, maxMinTong)
// 判断一下,如果计算出来的(如:2019-6-17)大于最后的日期(如2019-6-30)就需要减去(2019-6-30到2019-6-17的天数,否则不需要)
if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
let prevMonth = new Date(maxMinTong).getMonth() - 1
let lastTime=new Date(maxMinTong).setMonth(prevMonth)
dffeDay1 = dffeDay1 - getDay((new Date(lastTime).getFullYear() + '/' + (new Date(lastTime).getMonth()+1) + '/' + new Date(lastTime).getDate()), maxMinTong)
}
let dffeYear = getYear(time, twoTime)
let dffeMonth = getMonth(time, twoTime, dffeYear * 12)
let dffeDay = getDay(time, twoTime, dffeDay1)
let dffeHour = getHour(time, twoTime, getDay(time, twoTime) * 24)
let dffeMinute = getMinute(time, twoTime, (getDay(time, twoTime) * 24 * 60 + dffeHour * 60))
let dffeSecond = getSecond(time, twoTime, (getDay(time, twoTime) * 24 * 60 * 60 + dffeHour * 60 * 60 + dffeMinute * 60))
console.log(`年:${dffeYear},月:${dffeMonth},日:${dffeDay},时:${dffeHour},分:${dffeMinute},秒:${dffeSecond}`)
return { dffeYear, dffeMonth, dffeDay, dffeHour, dffeMinute, dffeSecond }
}
// 相差年月周日时分秒
const getDffeYMWDHMS = (time, twoTime) => {
// 计算出与最小日期的日时分相同的最大日期的值
const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, 'day')
let dffeDay1 = getDay(minTime, maxMinTong)
// 如果大于了,就需要减掉多余天数
if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
let prevMonth = new Date(maxMinTong).getMonth() - 1
let lastTime=new Date(maxMinTong).setMonth(prevMonth)
dffeDay1 = dffeDay1 - getDay((new Date(lastTime).getFullYear() + '/' + (new Date(lastTime).getMonth()+1) + '/' + new Date(lastTime).getDate()), maxMinTong)
}
let dffeYear = getYear(time, twoTime)
let dffeMonth = getMonth(time, twoTime, dffeYear * 12)
let dffeDayZong = getDay(time, twoTime, dffeDay1)
let dffeWeek = parseInt(dffeDayZong / 7)
let dffeDay = dffeDayZong - dffeWeek * 7
let dffeHour = getHour(time, twoTime, getDay(time, twoTime) * 24)
let dffeMinute = getMinute(time, twoTime, (getDay(time, twoTime) * 24 * 60 + dffeHour * 60))
let dffeSecond = getSecond(time, twoTime, (getDay(time, twoTime) * 24 * 60 * 60 + dffeHour * 60 * 60 + dffeMinute * 60))
console.log(`年:${dffeYear},月:${dffeMonth},周:${dffeWeek},日:${dffeDay},时:${dffeHour},分:${dffeMinute},秒:${dffeSecond}`)
return { dffeYear, dffeMonth, dffeWeek, dffeDay, dffeHour, dffeMinute, dffeSecond }
}
let chaYear = getYear(time, twoTime)
let chaMonth = getMonth(time, twoTime)
let chaDay = getDay(time, twoTime)
let chaHour = getHour(time, twoTime)
let chaMinute = getMinute(time, twoTime)
let chaSecond = getSecond(time, twoTime)
return {
chaYear, chaMonth, chaDay, chaHour, chaMinute, chaSecond,
getDffeYMDHMS: getDffeYMDHMS(time, twoTime),
getDffeYMWDHMS: getDffeYMWDHMS(time, twoTime)
}
}
纯html+js编程,前端存储状态数据
相关笔记:
- https://blog.csdn.net/ITsmallchen/article/details/109398588
- https://juejin.cn/post/7203569814181986364
项目错误笔记
修改一个对象的值,另一个对象也改变
当两个对象引用了同一个对象时,一个对象的值发生变动,另一个也会发生改变
解决方法:将对象想办法copy一份再赋值,让两个对象不再使用同一个内存地址
例如:将对象转换为 json字符串
,再转换回来
目标对象 = JSON.parse(JSON.stringify(源对象))
数字 Infinity
数字计算的结果为Infinity是因为计算错了
因为某个值或因为某一些值的原因,导致计算结果过大。需要仔细排查