JS 날짜 실 용적 방법
23561 단어 js
var DateUtil = function(){
/**
*
* @param date Date
* @return boolean true false
*/
this.isLeapYear = function(date){
return (0==date.getYear()%4&&((date.getYear()%100!=0)||(date.getYear()%400==0)));
}
/**
*
* @param f , yyyy-MM-dd HH:mm:ss
* @param date Date , ,
*
* YYYY/yyyy/YY/yy
* MM/M
* W/w
* dd/DD/d/D
* hh/HH/h/H
* mm/m
* ss/SS/s/S
* @return string
*/
this.dateToStr = function(formatStr, date){
formatStr = arguments[0] || "yyyy-MM-dd HH:mm:ss";
date = arguments[1] || new Date();
var str = formatStr;
var Week = [' ',' ',' ',' ',' ',' ',' '];
str=str.replace(/yyyy|YYYY/,date.getFullYear());
str=str.replace(/yy|YY/,(date.getYear() % 100)>9?(date.getYear() % 100).toString():'0' + (date.getYear() % 100));
str=str.replace(/MM/,date.getMonth()>9?(date.getMonth() + 1):'0' + (date.getMonth() + 1));
str=str.replace(/M/g,date.getMonth());
str=str.replace(/w|W/g,Week[date.getDay()]);
str=str.replace(/dd|DD/,date.getDate()>9?date.getDate().toString():'0' + date.getDate());
str=str.replace(/d|D/g,date.getDate());
str=str.replace(/hh|HH/,date.getHours()>9?date.getHours().toString():'0' + date.getHours());
str=str.replace(/h|H/g,date.getHours());
str=str.replace(/mm/,date.getMinutes()>9?date.getMinutes().toString():'0' + date.getMinutes());
str=str.replace(/m/g,date.getMinutes());
str=str.replace(/ss|SS/,date.getSeconds()>9?date.getSeconds().toString():'0' + date.getSeconds());
str=str.replace(/s|S/g,date.getSeconds());
return str;
}
/**
*
* @param strInterval string y m d w ww h n s
* @param num int
* @param date Date
* @return Date
*/
this.dateAdd = function(strInterval, num, date){
date = arguments[2] || new Date();
switch (strInterval) {
case 's' :return new Date(date.getTime() + (1000 * num));
case 'n' :return new Date(date.getTime() + (60000 * num));
case 'h' :return new Date(date.getTime() + (3600000 * num));
case 'd' :return new Date(date.getTime() + (86400000 * num));
case 'w' :return new Date(date.getTime() + ((86400000 * 7) * num));
case 'm' :return new Date(date.getFullYear(), (date.getMonth()) + num, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
case 'y' :return new Date((date.getFullYear() + num), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
}
}
/**
* dtEnd
* @param strInterval string y m d w ww h n s
* @param dtStart Date y m d w ww h n s
* @param dtEnd Date y m d w ww h n s
*/
this.dateDiff = function(strInterval, dtStart, dtEnd) {
switch (strInterval) {
case 's' :return parseInt((dtEnd - dtStart) / 1000);
case 'n' :return parseInt((dtEnd - dtStart) / 60000);
case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);
case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear();
}
}
/**
*
* @param date Date yyyy-MM-dd HH:mm:ss, ,
*/
this.strToDate = function(dateStr){
var data = dateStr;
var reCat = /(\d{1,4})/gm;
var t = data.match(reCat);
t[1] = t[1] - 1;
eval('var d = new Date('+t.join(',')+');');
return d;
}
/**
* yyyy-MM-dd HH:mm:ss
*
*/
this.strFormatToDate = function(formatStr, dateStr){
var year = 0;
var start = -1;
var len = dateStr.length;
if((start = formatStr.indexOf('yyyy')) > -1 && start < len){
year = dateStr.substr(start, 4);
}
var month = 0;
if((start = formatStr.indexOf('MM')) > -1 && start < len){
month = parseInt(dateStr.substr(start, 2)) - 1;
}
var day = 0;
if((start = formatStr.indexOf('dd')) > -1 && start < len){
day = parseInt(dateStr.substr(start, 2));
}
var hour = 0;
if( ((start = formatStr.indexOf('HH')) > -1 || (start = formatStr.indexOf('hh')) > 1) && start < len){
hour = parseInt(dateStr.substr(start, 2));
}
var minute = 0;
if((start = formatStr.indexOf('mm')) > -1 && start < len){
minute = dateStr.substr(start, 2);
}
var second = 0;
if((start = formatStr.indexOf('ss')) > -1 && start < len){
second = dateStr.substr(start, 2);
}
return new Date(year, month, day, hour, minute, second);
}
/**
*
*/
this.dateToLong = function(date){
return date.getTime();
}
/**
*
* @param dateVal number
*/
this.longToDate = function(dateVal){
return new Date(dateVal);
}
/**
*
* @param str string
* @param formatStr string , yyyy-MM-dd
*/
this.isDate = function(str, formatStr){
if (formatStr == null){
formatStr = "yyyyMMdd";
}
var yIndex = formatStr.indexOf("yyyy");
if(yIndex==-1){
return false;
}
var year = str.substring(yIndex,yIndex+4);
var mIndex = formatStr.indexOf("MM");
if(mIndex==-1){
return false;
}
var month = str.substring(mIndex,mIndex+2);
var dIndex = formatStr.indexOf("dd");
if(dIndex==-1){
return false;
}
var day = str.substring(dIndex,dIndex+2);
if(!isNumber(year)||year>"2100" || year< "1900"){
return false;
}
if(!isNumber(month)||month>"12" || month< "01"){
return false;
}
if(day>getMaxDay(year,month) || day< "01"){
return false;
}
return true;
}
this.getMaxDay = function(year,month) {
if(month==4||month==6||month==9||month==11)
return "30";
if(month==2)
if(year%4==0&&year%100!=0 || year%400==0)
return "29";
else
return "28";
return "31";
}
/**
*
*/
this.isNumber = function(str)
{
var regExp = /^\d+$/g;
return regExp.test(str);
}
/**
* [ 、 、 、 、 、 ]
*/
this.toArray = function(myDate)
{
myDate = arguments[0] || new Date();
var myArray = Array();
myArray[0] = myDate.getFullYear();
myArray[1] = myDate.getMonth();
myArray[2] = myDate.getDate();
myArray[3] = myDate.getHours();
myArray[4] = myDate.getMinutes();
myArray[5] = myDate.getSeconds();
return myArray;
}
/**
*
* interval
* y M d w ww h n s
*/
this.datePart = function(interval, myDate)
{
myDate = arguments[1] || new Date();
var partStr='';
var Week = [' ',' ',' ',' ',' ',' ',' '];
switch (interval)
{
case 'y' :partStr = myDate.getFullYear();break;
case 'M' :partStr = myDate.getMonth()+1;break;
case 'd' :partStr = myDate.getDate();break;
case 'w' :partStr = Week[myDate.getDay()];break;
case 'ww' :partStr = myDate.WeekNumOfYear();break;
case 'h' :partStr = myDate.getHours();break;
case 'm' :partStr = myDate.getMinutes();break;
case 's' :partStr = myDate.getSeconds();break;
}
return partStr;
}
/**
*
*/
this.maxDayOfDate = function(date)
{
date = arguments[0] || new Date();
date.setDate(1);
date.setMonth(date.getMonth() + 1);
var time = date.getTime() - 24 * 60 * 60 * 1000;
var newDate = new Date(time);
return newDate.getDate();
}
return this;
}();
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[2022.04.19] 자바스크립트 this - 생성자 함수와 이벤트리스너에서의 this18일에 this에 대해 공부하면서 적었던 일반적인 함수나 객체에서의 this가 아닌 오늘은 이벤트리스너와 생성자 함수 안에서의 this를 살펴보기로 했다. new 키워드를 붙여 함수를 생성자로 사용할 때 this는...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.