Created
September 6, 2019 01:13
-
-
Save 4ox/c579c9fb2e93ae19e955f05b450a0978 to your computer and use it in GitHub Desktop.
Javascript Date Format prototype Extention
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Date.prototype | |
Date.prototype.Format = function( str ){ | |
var date = this; | |
return str.replace(/%(Y|y|n|m|d|j|a|A|g|G|h|H|i|s|DD|ll|D|l|%)/g, function( word ){ | |
switch( word ){ | |
case "%%": return '%'; break; | |
case "%Y": return date.getFullYear(); break; | |
case "%y": return String(date.getFullYear()).substring(2,4); break; | |
case "%n": return Number(date.getMonth())+1; break; | |
case "%m": return (date.getMonth() < 9 ? "0" : "" )+(date.getMonth()+1); break; | |
case "%d": return (date.getDate() < 10 ? "0" : "" )+date.getDate(); break; | |
case "%j": return date.getDate(); break; | |
case "%a": return (date.getHours() < 12 ? "am":"pm"); break; | |
case "%A": return (date.getHours() < 12 ? "오전":"오후"); break; | |
case "%g": return (date.getHours() < 12 ? date.getHours() : Number(date.getHours())-12); break; | |
case "%G": return date.getHours(); break; | |
case "%h": var tmp = (date.getHours() < 12 ? date.getHours() : Number(date.getHours())-12); return (tmp < 10 ? "0":"" )+tmp; break; | |
case "%H": return (date.getHours() < 10 ? "0" : "" )+date.getHours(); break; | |
case "%i": return (date.getMinutes() < 10 ? "0" : "" )+date.getMinutes(); break; | |
case "%s": return (date.getSeconds() < 10 ? "0" : "" )+date.getSeconds(); break; | |
case "%D": return ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][date.getDay()]; break; | |
case "%l": return ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][date.getDay()]; break; | |
case "%DD": return ["일","월","화","수","목","금","토"][date.getDay()]; break; | |
case "%ll": return ["일요일","월요일","화요일","수요일","목요일","금요일","토요일"][date.getDay()]; break; | |
default : return word; | |
} | |
}); | |
}; | |
Date.prototype.Day = function( num ) { | |
if( !num ) { return this; }; | |
return new Date( this.getTime() + 86400000 * num ); | |
} | |
Date.lastDate = function (y,m) { | |
var count = 31; | |
if (m == 2) { count = ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) ? 29 : 28; } | |
else if (m == 4 || m == 6 || m == 9 || m == 11) { count = 30; } | |
return count; | |
}; | |
Date.prototype.Month = function( num ) { | |
if( !num ) { return this; }; | |
var D = this; | |
var y = D.getFullYear(); | |
var m = D.getMonth()+1; | |
var d = D.getDate(); | |
var month = m + num; | |
var abs = Math.abs( month ); | |
//+ or - | |
if( month > 0 ) { | |
y = y + ~~( ( month - 1 ) / 12 ); | |
m = abs % 12 == 0 ? 12 : abs % 12; | |
} | |
else { | |
y = y + ~~( month / 12 ) - 1; | |
m = 12 - abs % 12; | |
} | |
//28~31day | |
var lastDate = Date.lastDate(y,m); | |
if( d > lastDate ) { d = lastDate } | |
return new Date( y + '/' + m + '/' + d ); | |
}; | |
Date.prototype.moveDay = function( num ) { | |
if( !num || num == 0 ) { return this; }; | |
return new Date( this.getTime() + 86400000 * num ); | |
} | |
Date.prototype.moveWeek = function( num ) { | |
if( !num ) { return this; }; | |
return this.Day( 7 * num ); | |
}; | |
Date.prototype.nextWeek = function(num) { return this.moveWeek((num||1)*1); } | |
Date.prototype.lastWeek = function(num) { return this.moveWeek((num||1)*-1); } | |
Date.prototype.moveMonth = function( num ) { | |
if( !num ) { return this; }; | |
var D = this; | |
var y = D.getFullYear(); | |
var m = D.getMonth()+1; | |
var d = D.getDate(); | |
var month = m + num; | |
var abs = Math.abs( month ); | |
//+ or - | |
if( month > 0 ) { | |
y = y + ~~( ( month - 1 ) / 12 ); | |
m = abs % 12 == 0 ? 12 : abs % 12; | |
} | |
else { | |
y = y + ~~( month / 12 ) - 1; | |
m = 12 - abs % 12; | |
} | |
//28~31day | |
var lastDate = Date.lastDate(y,m); | |
if( d > lastDate ) { d = lastDate } | |
var ret = new Date( y + '/' + m + '/' + d ); | |
ret.setHours( D.getHours() ); | |
ret.setMinutes( D.getMinutes() ); | |
ret.setSeconds( D.getSeconds() ); | |
return ret; | |
}; | |
Date.prototype.nextMonth = function(num) { return this.moveMonth((num||1)*1); } | |
Date.prototype.lastMonth = function(num) { return this.moveMonth((num||1)*-1); } | |
Date.prototype.moveYear = function( num ) { | |
if( !num ) { return this; }; | |
var D = this; | |
var y = D.getFullYear(); | |
var m = D.getMonth()+1; | |
var d = D.getDate(); | |
y = y + num; | |
y = y < 1970 ? 1970 : y; //after 1970 | |
//28~31day | |
var lastDate = Date.lastDate(y,m); | |
if( d > lastDate ) { d = lastDate } | |
var ret = new Date( y + '/' + m + '/' + d ); | |
ret.setHours( D.getHours() ); | |
ret.setMinutes( D.getMinutes() ); | |
ret.setSeconds( D.getSeconds() ); | |
return ret; | |
}; | |
Date.prototype.nextYear = function(num) { return this.moveYear((num||1)*1); } | |
Date.prototype.lastYear = function(num) { return this.moveYear((num||1)*-1); } | |
Date.prototype.UTC = function() { | |
if( Date.UTC ) { | |
return new Date(Date.UTC(this.getFullYear(),this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds())); | |
} | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment