날짜 처리 공통 함수
8757 단어 prototype
var DateUtils_MD = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
/** */
function DateUtils() {}
DateUtils.prototype.toDate = function(strDate) {
if (strDate.length == 19) { // YYYY-MM-DD HH:MI:SS
var year = strDate.substring(0, 4);
var month = strDate.substring(5, 7);
var date = strDate.substring(8, 10);
var hour = strDate.substring(11, 13);
var min = strDate.substring(14, 16);
var sec = strDate.substring(17, 19);
return new Date(year, month - 1, date, hour, min, sec);
} else if (strDate.length == 10) { // "YYYY-MM-DD"
var year = strDate.substring(0, 4);
var month = strDate.substring(5, 7);
var date = strDate.substring(8, 10);
return new Date(year, month - 1, date);
} else if (strDate.length == 7) { // "YYYY-MM"
var year = strDate.substring(0, 4);
var month = strDate.substring(5, 7);
return new Date(year, month - 1);
} else if (strDate.length == 4) { // "YYYY"
var year = strDate.substring(0, 4);
return new Date(year);
} else {
alert("DateUtils.toDate(strDate) error! invalid argument(format).");
}
}
DateUtils.prototype.toString = function(date, format) {
var strDate;
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
month = (parseInt(month) < 10) ? ("0" + month) : (month);
day = (parseInt(day) < 10) ? ("0" + day ) : (day);
hour = (parseInt(hour) < 10) ? ("0" + hour) : (hour);
min = (parseInt(min) < 10) ? ("0" + min) : (min);
sec = (parseInt(sec) < 10) ? ("0" + sec) : (sec);
if ("YYYY-MM-DD HH:MI:SS" == format) {
strDate = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
} else if ("YYYY-MM-DD" == format) {
strDate = year + "-" + month + "-" + day;
} else if ("YYYY-MM" == format) {
strDate = year + "-" + month;
} else if ("YYYY" == format) {
strDate = year;
} else {
alert("DateUtils.toString(date, format) error! invalid argument(format).");
}
return strDate;
}
DateUtils.prototype.getMonthDays = function(date,month) {
var year = date.getFullYear();
if (typeof month == "undefined") {
month = date.getMonth();
}
if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) {
return 29;
} else {
return DateUtils_MD[month];
}
};
DateUtils.prototype.addDays = function(dayOffset, strBaseDate) {
var date = (arguments.length == 1) ? this.toDate(this.today()) : this.toDate(strBaseDate);
date = new Date(date.getTime() + parseInt(dayOffset) * 24 * 3600 * 1000);
return this.toString(new Date(date), "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.addMonths = function(monthOffset, strBaseDate) {
var date = (arguments.length == 1) ? this.toDate(this.today()): this.toDate(strBaseDate);
var month=date.getMonth();
var cd=date.getDate();//this.getMonthDays(date,month);
var td=this.getMonthDays(date,date.getMonth() + parseInt(monthOffset));
if(cd > td){date.setDate(td);}
date.setMonth(date.getMonth() + parseInt(monthOffset));
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.addMonthsForStart = function(monthOffset, strBaseDate) {
var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
strDate = this.addMonths(monthOffset, strDate);
return this.firstDayOfMonth(strDate);
}
DateUtils.prototype.addMonthsForEnd = function(monthOffset, strBaseDate) {
var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
strDate = this.addMonths(monthOffset, strDate);
return this.addDays(-1, this.firstDayOfMonth(strDate));
}
DateUtils.prototype.addYears = function(yearOffset, strBaseDate) {
var date = (arguments.length == 1) ? this.toDate(this.today()) : this.toDate(strBaseDate);
date.setYear(date.getYear() + parseInt(yearOffset));
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.addYearsForStart = function(yearOffset, strBaseDate) {
var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
strDate = this.addYears(yearOffset, strDate);
return this.firstDayOfYear(strDate);
}
DateUtils.prototype.addYearsForEnd = function(yearOffset, strBaseDate) {
var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
strDate = this.addYears(yearOffset, strDate);
return this.firstDayOfYear(strDate);
}
DateUtils.prototype.sunOfWeek = function(strDate) {
var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
date = new Date(date - (date.getDay()) * (24 * 3600 * 1000));
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.monOfWeek = function(strDate) {
var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
date = new Date(date - (date.getDay() - 1) * (24 * 3600 * 1000));
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.tueOfWeek = function(strDate) {
var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
date = new Date(date - (date.getDay() - 2) * (24 * 3600 * 1000));
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.wedOfWeek = function(strDate) {
var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
date = new Date(date - (date.getDay() - 3) * (24 * 3600 * 1000));
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.turOfWeek = function(strDate) {
var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
date = new Date(date - (date.getDay() - 4) * (24 * 3600 * 1000));
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.friOfWeek = function(strDate) {
var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
date = new Date(date - (date.getDay() - 5) * (24 * 3600 * 1000));
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.satOfWeek = function(strDate) {
var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
date = new Date(date - (date.getDay() - 6) * (24 * 3600 * 1000));
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.firstDayOfMonth = function(strDate) {
var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
date.setDate(1);
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.lastDayOfMonth = function(strDate) {
strDate = (arguments.length == 0) ? this.today() : (strDate);
strDate = this.addMonths(1, strDate);
strDate = this.firstDayOfMonth(strDate);
strDate = this.addDays(-1, strDate);
return strDate;
}
DateUtils.prototype.firstDayOfYear = function(strDate) {
var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
date.setMonth(0);
date.setDate(1);
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.lastDayOfYear = function(strDate) {
var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
date.setMonth(11);
date.setDate(31);
return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}
DateUtils.prototype.today = function(format) {
if(getToday && typeof(getToday)=="function"){
return getToday();
}else{
if (arguments.length == 0) {
return this.toString(new Date(), "YYYY-MM-DD");
} else {
return this.toString(new Date(), format);
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기능 재검토(프로토타입 아님) 🤥빠른 수정을 위한 몇 가지 참고 사항 사용자 지정 속성이 있는 함수 이것은 대부분의 경우 런타임 바인딩이므로 someKey는 aFunction 또는 aFunction.prototype의 속성이 아닙니다. 접두사 cu...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.