[javascript] calculate datetime

Posted by nopsled
2018.04.24 17:39 프로그래밍

Calculate datetime 2 ways.



1 way will be calculate about YYYY-MM - YYYY-MM


function calMonthRange(val1, val2)
{
var FORMAT = "-";
if (val1.length != 7 || val2.length != 7)
return null;
if (val1.indexOf(FORMAT) < 0 || val2.indexOf(FORMAT) < 0)
return null;
var start_dt = val1.split(FORMAT);
var end_dt = val2.split(FORMAT);
start_dt[1] = (Number(start_dt[1]) - 1) + "";
end_dt[1] = (Number(end_dt[1]) - 1) + "";
var from_dt = new Date(start_dt[0], start_dt[1]);
var to_dt = new Date(end_dt[0], end_dt[1]);
return Math.ceil((to_dt.getTime() - from_dt.getTime()) / 1000 / 60 / 60 / 24 / 30);
}

calMonthRange("2017-08", "2018-04")


/*
Debugging output
*/
9




/*
1 way apply code
*/

function formatMonth(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
year = d.getFullYear();

if (month.length < 2) month = '0' + month;

return [year, month].join('-');
}

function calMonthRange(val1, val2)
{
var FORMAT = "-";
if (val1.length != 7 || val2.length != 7)
return null;
if (val1.indexOf(FORMAT) < 0 || val2.indexOf(FORMAT) < 0)
return null;
var start_dt = val1.split(FORMAT);
var end_dt = val2.split(FORMAT);
start_dt[1] = (Number(start_dt[1]) - 1) + "";
end_dt[1] = (Number(end_dt[1]) - 1) + "";
var from_dt = new Date(start_dt[0], start_dt[1]);
var to_dt = new Date(end_dt[0], end_dt[1]);
return Math.ceil((to_dt.getTime() - from_dt.getTime()) / 1000 / 60 / 60 / 24 / 30);
}

for(var i=0; i<calMonthRange("2017-08", "2018-04"); i++) {
    console.log(formatMonth(new Date("2017-08").setMonth(new Date("2017-08").getMonth() + i)))
}



/*
Debugging output
*/
2017-08
2017-09
2017-10
2017-11
2017-12
2018-01
2018-02
2018-03
2018-04




2 way will be calculate about YYYY-MM-DD - YYYY-MM-DD

function calDateRange(val1, val2)
{
var FORMAT = "-";
if (val1.length != 10 || val2.length != 10)
return null;
if (val1.indexOf(FORMAT) < 0 || val2.indexOf(FORMAT) < 0)
return null;
var start_dt = val1.split(FORMAT);
var end_dt = val2.split(FORMAT);
start_dt[1] = (Number(start_dt[1]) - 1) + "";
end_dt[1] = (Number(end_dt[1]) - 1) + "";
var from_dt = new Date(start_dt[0], start_dt[1], start_dt[2]);
var to_dt = new Date(end_dt[0], end_dt[1], end_dt[2]);
return (to_dt.getTime() - from_dt.getTime()) / 1000 / 60 / 60 / 24;
}

calDateRange("2018-03-22", "2018-04-24")


/*
Debugging output
*/
33




/*
2 way apply code
*/

function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();

if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;

return [year, month, day].join('-');
}

function calDateRange(val1, val2)
{
var FORMAT = "-";
if (val1.length != 10 || val2.length != 10)
return null;
if (val1.indexOf(FORMAT) < 0 || val2.indexOf(FORMAT) < 0)
return null;
var start_dt = val1.split(FORMAT);
var end_dt = val2.split(FORMAT);
start_dt[1] = (Number(start_dt[1]) - 1) + "";
end_dt[1] = (Number(end_dt[1]) - 1) + "";
var from_dt = new Date(start_dt[0], start_dt[1], start_dt[2]);
var to_dt = new Date(end_dt[0], end_dt[1], end_dt[2]);
return (to_dt.getTime() - from_dt.getTime()) / 1000 / 60 / 60 / 24;
}

for(var i=0; i<calDateRange("2018-03-22", "2018-04-24"); i++) {
console.log(formatDate(new Date("2018-03-22").setDate(new Date("2018-03-22").getDate() + i)))
}



/*
Debugging output
*/
2018-03-22
2018-03-23
2018-03-24
2018-03-25
2018-03-26
2018-03-27
2018-03-28
2018-03-29
2018-03-30
2018-03-31
2018-04-01
2018-04-02
2018-04-03
2018-04-04
2018-04-05
2018-04-06
2018-04-07
2018-04-08
2018-04-09
2018-04-10
2018-04-11
2018-04-12
2018-04-13
2018-04-14
2018-04-15
2018-04-16
2018-04-17
2018-04-18
2018-04-19
2018-04-20
2018-04-21
2018-04-22
2018-04-23




'프로그래밍' 카테고리의 다른 글

python pyodbc 설치 탐험기  (0) 2018.06.04
[javascript] calculate datetime  (0) 2018.04.24
[javascript] get ip only using javascript  (5) 2018.04.19
python ctypes GetFileVersion  (0) 2016.11.09
python 윈도우 한글문제  (0) 2016.04.26
python unicode unescape (html unescape)  (0) 2015.11.07
이 댓글을 비밀 댓글로