일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- JS
- Git
- s3
- sample
- 설정
- MySQL
- 엘라스틱서치
- docker
- ChatGPT
- Kibana
- JavaScript
- MSSQL
- 유니티
- API
- Ai
- error
- unity
- mariadb
- build
- Windows
- logstash
- AWS
- 구글
- Python
- nodejs
- Linux
- ssh
- elasticsearch
- 영어
- Today
- Total
가끔 보자, 하늘.
자주 사용하는 시간 관련 코드들 본문
자주 사용하는 기능들 모아서 정리한 코드입니다.
필요하신 분들은 가져다 그대로 사용하시면 됩니다.
Date.prototype.mmddyyyy = function() {
return (this.getMonth() + 1) +
"/" + this.padZero(this.getDate()) +
"/" + this.padZero(this.getFullYear());
};
Date.prototype.yyyymmdd = function() {
return this.getFullYear() +
"/" + this.padZero((this.getMonth() + 1)) +
"/" + this.padZero(this.getDate());
};
Date.prototype.mmddyyyytime = function(){
return (this.getMonth() + 1) +
"/" + this.padZero(this.getDate()) +
"/" + this.padZero(this.getFullYear()) +
" " + this.padZero(this.getHours()) +
":" + this.padZero(this.getMinutes()) +
":" + this.padZero(this.getSeconds());
};
Date.prototype.yyyymmddfordb = function() {
var yyyy = this.getFullYear();
var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based
var dd = this.getDate() < 10 ? "0" + this.getDate() : this.getDate();
return "".concat(yyyy).concat("-").concat(mm).concat("-").concat(dd);
};
Date.prototype.yyyymmddtime = function(){
return this.getFullYear() +
"/" + this.padZero((this.getMonth() + 1)) +
"/" + this.padZero(this.getDate())+
" " + this.padZero(this.getHours()) +
":" + this.padZero(this.getMinutes()) +
":" + this.padZero(this.getSeconds());
};
Date.prototype.yyyymmddstarttime = function(){
return this.getFullYear() +
"/" + this.padZero((this.getMonth() + 1)) +
"/" + this.padZero(this.getDate())+
" " + "00:00:00";
};
Date.prototype.yyyymmddendtime = function(){
return this.getFullYear() +
"/" + this.padZero((this.getMonth() + 1)) +
"/" + this.padZero(this.getDate())+
" " + "23:59:59";
};
Date.prototype.padZero =function (n) {
n = n + '';
return n.length >= 2 ? n : new Array(2 - n.length + 1).join('0') + n;
}
var util_date = {
getToday_yyyymmdd: function(){
var today = new Date();
return today.yyyymmdd();
},
get_yyyymmdd:function(_date){
if( _date == "" || _date == null || _date == undefined ){
return null;
}else{
var date = new Date(_date);
return date.yyyymmdd();
}
},
getToday_yyyymmddtime:function(_date){
if( _date == "" || _date == null || _date == undefined ){
return null;
}else{
var date = new Date(_date);
return date.yyyymmddtime();
}
},
get_yyyymmddtime:function(_date){
if( _date == "" || _date == null || _date == undefined ){
return null;
}else{
var date = new Date(_date);
return date.yyyymmddtime();
}
},
getThisMonday: function(_date){
var d = new Date(_date);
var day = d.getDay(),
diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
return new Date(d.setDate(diff));
},
getLastMonday: function(_date){
var d = this.getThisMonday(_date);
return this.getNextDay(d,-7);
},
getNextMonday: function(_date){
var d = this.getThisMonday(_date);
return this.getNextDay(d,7);
},
getNextDay: function(_date, _count){
var d = new Date(_date);
d.setDate(d.getDate() + _count);
return new Date(d);
},
getMonday:function(d, weeksago) {
var day = d.getDay(),
diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
var monday = new Date(d.setDate(diff));
monday.setDate( monday.getDate()- 7*weeksago);
return monday;
},
getFirstday: function(d){
var t = new Date(d);
return new Date(t.getFullYear(), t.getMonth(), 1);
},
getFirstdayOfLastMonth: function(d){
var t = this.getLastdayOfLastMonth(d,0);
return new Date(t.getFullYear(), t.getMonth(), 1);
},
getLastdayOfLastMonth: function(d, monthsago){
var t = new Date(d);
t.setMonth( t.getMonth()-monthsago);
t = new Date(t.getFullYear(), t.getMonth(), 0);
return t;
},
getLastdayOfThisMonth:function(d){
var t = new Date(d);
return new Date(t.getFullYear(), t.getMonth() + 1, 0);
},
getFirstdayOfNextMonth: function(d){
var t = this.getLastdayOfThisMonth(d);
return this.getNextDay(t, 1);
},
getDiffDays: function(f , s){
var date1 = new Date(f);
var date2 = new Date(s);
var timediff = Math.abs( date1.getTime() - date2.getTime() );
return Math.ceil( timediff/(1000*3600*24));
},
bThisMonth: function ( d ){
var today = new Date();
var date1 = new Date(d);
if(today.getFullYear() == date1.getFullYear() && today.getMonth() == date1.getMonth())
return true;
return false;
},
bToday: function ( d ){
var today = new Date();
var date1 = new Date(d);
if(today.getFullYear() == date1.getFullYear() && today.getMonth() == date1.getMonth() && today.getDay() == date1.getDay())
return true;
return false;
},
bThesedays: function(d){
var today = new Date();
var date1 = new Date(d);
var diff = this.getDiffDays( today, date1);
if(diff < 5)
return true;
return false;
}
};
module.exports = util_date;
'개발 이야기 > 개발 및 서비스' 카테고리의 다른 글
React native 스터디 정리 - react native navigation Setting (0) | 2019.03.05 |
---|---|
라이센스 라이브러리 관련 Android Build error (0) | 2019.02.15 |
Building a react-native project (52) | 2018.11.06 |
개발에 대한 knowhow (0) | 2018.10.29 |
Elasticsearch를 Node.js에 통합하기 (178) | 2018.10.01 |