반응형
250x250
Notice
Recent Posts
Recent Comments
Link
«   2024/03   »
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
Archives
Today
Total
관리 메뉴

가끔 보자, 하늘.

자주 사용하는 시간 관련 코드들 본문

개발 이야기/개발 및 서비스

자주 사용하는 시간 관련 코드들

가온아 2018. 12. 6. 17:05

자주 사용하는 기능들 모아서 정리한 코드입니다. 


필요하신 분들은 가져다 그대로 사용하시면 됩니다. 



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;


반응형