该脚本是用于在js中计算各种类型的同环比时间,输入时间格式为 yyyy-MM,输出为各种格式的同环比时间字符串。
//拆解出查询条件中的年、月
var datetime = new Date(selected_month_time)
var year = datetime.getFullYear()
var month = datetime.getMonth() + 1
//当天凌晨时间戳,并拆解出当天年、月、日
var todayDateTime = new Date(new Date().toLocaleDateString())
var todayYear = todayDateTime.getFullYear()
var todayMonth = todayDateTime.getMonth() + 1
var todayDay = todayDateTime.getDate()
//判断查询是否为当月
var isCurrentMonth = false
if( year===todayYear && month===todayMonth){
isCurrentMonth = true
}
//查询月份的起止时间
var selected_start_date = selected_month_time + '-01 00:00:00'
var selected_stop_date = addMonthOrGetNow(year,month,isCurrentMonth,todayYear,todayMonth,todayDay)
var selected_start_date_day = selected_start_date.substring(0,10)
var selected_stop_date_day = selected_stop_date.substring(0,10)
var selected_start_date_month = selected_start_date.substring(0,7)
var selected_stop_date_month = getStopMonth(isCurrentMonth,selected_stop_date.substring(0,7))
console.log("selected_start_date",selected_start_date);
console.log("selected_stop_date",selected_stop_date);
console.log("selected_start_date_day",selected_start_date_day);
console.log("selected_stop_date_day",selected_stop_date_day);
console.log("selected_start_date_month",selected_start_date_month);
console.log("selected_stop_date_month",selected_stop_date_month);
console.log("bq_month",month);
console.log("bq_last_day",getLastDay(year,month));
//同比查询月份的起止时间
var tb_selected_start_date = tbDateStr(selected_start_date)
var tb_selected_stop_date = tbDateStr(selected_stop_date)
var tb_selected_start_date_day = tb_selected_start_date.substring(0,10)
var tb_selected_stop_date_day = tb_selected_stop_date.substring(0,10)
var tb_selected_start_date_month = tb_selected_start_date.substring(0,7)
var tb_selected_stop_date_month = getStopMonth(isCurrentMonth,tb_selected_stop_date.substring(0,7))
console.log("tb_selected_start_date",tb_selected_start_date);
console.log("tb_selected_stop_date",tb_selected_stop_date);
console.log("tb_selected_start_date_day",tb_selected_start_date_day);
console.log("tb_selected_stop_date_day",tb_selected_stop_date_day);
console.log("tb_selected_start_date_month",tb_selected_start_date_month);
console.log("tb_selected_stop_date_month",tb_selected_stop_date_month);
//环比查询月份的起止时间
var hb_selected_start_date = hbDateStr(selected_start_date)
var hb_selected_stop_date = hbDateStr(selected_stop_date)
var hb_selected_start_date_day = hb_selected_start_date.substring(0,10)
var hb_selected_stop_date_day = hb_selected_stop_date.substring(0,10)
var hb_selected_start_date_month = hb_selected_start_date.substring(0,7)
var hb_selected_stop_date_month = getStopMonth(isCurrentMonth,hb_selected_stop_date.substring(0,7))
console.log("hb_selected_start_date",hb_selected_start_date);
console.log("hb_selected_stop_date",hb_selected_stop_date);
console.log("hb_selected_start_date_day",hb_selected_start_date_day);
console.log("hb_selected_stop_date_day",hb_selected_stop_date_day);
console.log("hb_selected_start_date_month",hb_selected_start_date_month);
console.log("hb_selected_stop_date_month",hb_selected_stop_date_month);
//计算近一年的本期、同期、环期的起止时间(只到天)
var select_start_data_recent_oneyear = tb_selected_start_date.substring(0,10)
var select_stop_data_recent_oneyear = selected_start_date.substring(0,10)
console.log('select_start_data_recent_oneyear',select_start_data_recent_oneyear)
console.log('select_stop_data_recent_oneyear',select_stop_data_recent_oneyear)
var tb_select_start_data_recent_oneyear = tbDateStr(tb_selected_start_date).substring(0,10)
var tb_select_stop_data_recent_oneyear = select_start_data_recent_oneyear
console.log('tb_select_start_data_recent_oneyear',tb_select_start_data_recent_oneyear)
console.log('tb_select_stop_data_recent_oneyear',tb_select_stop_data_recent_oneyear)
var hb_select_start_data_recent_oneyear = tbDateStr(hbDateStr(selected_start_date)).substring(0,10)
var hb_select_stop_data_recent_oneyear = hbDateStr(selected_start_date).substring(0,10)
console.log('hb_select_start_data_recent_oneyear',hb_select_start_data_recent_oneyear)
console.log('hb_select_stop_data_recent_oneyear',hb_select_stop_data_recent_oneyear)
function addMonthOrGetNow(year, month,isCurrentMonth,todayYear,todayMonth,todayDay) {
if(isCurrentMonth){
if(todayMonth<10){
todayMonth = '0' + todayMonth
}
if(todayDay<10){
todayDay = '0' + todayDay
}
return '' + todayYear + '-' + todayMonth + '-' + todayDay + ' 00:00:00'
}else{
if(month===12){
year = year + 1
month = 1
}else{
month = month + 1
}
if(month<10){
month = '0' + month
}
return '' + year + '-' + month + '-01 00:00:00'
}
}
function tbDateStr(selected_date){
var datetime = new Date(selected_date)
var year = datetime.getFullYear()
year = year - 1
return '' + year + selected_date.substring(4)
}
function hbDateStr(selected_date){
var datetime = new Date(selected_date)
var year = datetime.getFullYear()
var month = datetime.getMonth() + 1
if(month===1){
year = year - 1
month = 12
}else{
month = month - 1
}
if(month<10){
month = '0' + month
}
return '' + year + '-' + month + selected_date.substring(7)
}
function getStopMonth(isCurrentMonth,stop_date_month) {
var year = parseInt(stop_date_month.substring(0,4))
var month = parseInt(stop_date_month.substring(5,7))
if(isCurrentMonth){
if(month==12){
month = 1
year = year + 1
}else{
month = month + 1
}
if(month<10){
return '' + year + '-0' + month
}else{
return '' + year + '-' + month
}
}else{
return stop_date_month
}
}
function getLastDay(year,month){
var lastDateTime = new Date(year,month,0)
var bq_last_day = lastDateTime.getDate()
if(bq_last_day<10){
return '0' + bq_last_day
}else{
return '' + bq_last_day
}
}
|