SQL语句获取当天,昨天,本周,上周,本月,上月的起始时间和结束时间

Posted 黄河大道东

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语句获取当天,昨天,本周,上周,本月,上月的起始时间和结束时间相关的知识,希望对你有一定的参考价值。

今天

SELECT DATE_FORMAT(NOW(),\'%Y-%m-%d 00:00:00\') AS \'今天开始\';
SELECT DATE_FORMAT(NOW(),\'%Y-%m-%d 23:59:59\') AS \'今天结束\';

昨天

SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 DAY), \'%Y-%m-%d 00:00:00\') AS \'昨天开始\';
SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 DAY), \'%Y-%m-%d 23:59:59\') AS \'昨天结束\';

本周

SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), \'%Y-%m-%d 00:00:00\') AS \'本周一\';
SELECT DATE_FORMAT( DATE_ADD(SUBDATE(CURDATE(), WEEKDAY(CURDATE())), INTERVAL 6 DAY), \'%Y-%m-%d 23:59:59\') AS \'本周末\';

上周

SELECT DATE_FORMAT( DATE_SUB( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), INTERVAL 1 WEEK), \'%Y-%m-%d 00:00:00\') AS \'上周一\';
SELECT DATE_FORMAT( SUBDATE(CURDATE(), WEEKDAY(CURDATE()) + 1), \'%Y-%m-%d 23:59:59\') AS \'上周末\';

本月

SELECT DATE_FORMAT( CURDATE(), \'%Y-%m-01 00:00:00\') AS \'本月初\';
SELECT DATE_FORMAT( LAST_DAY(CURDATE()), \'%Y-%m-%d 23:59:59\') AS \'本月末\';

上月

SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 MONTH), \'%Y-%m-01 00:00:00\') AS \'上月初\';
SELECT DATE_FORMAT( LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)), \'%Y-%m-%d 23:59:59\') AS \'上月末\';

JS时间 获取 当天,昨日,本周,上周,本月,上月

调用

setTimeRange (2);

 

function  setTimeRange (v) {
  var fmt = ‘YYYY-MM-DD HH:mm‘;
  var now = new Date();
  var end = new Date();
  if(v == 0){

  } else if(v == 1){

  } else if(v == 2){
    //昨日
    now.setDate(now.getDate() - 1);
    end.setDate(end.getDate() - 1);
  } else if(v == 3){
    //本周
    var nowDayOfWeek = now.getDay() ; //今天本周的第几天 
    if(nowDayOfWeek == 0) {
      nowDayOfWeek = 6; //周日
    } else {
      nowDayOfWeek = nowDayOfWeek - 1;
    }
    now.setDate(now.getDate() - nowDayOfWeek);
    end.setDate(now.getDate() + 6);
  } else if(v == 4){
    //上周
    var nowDayOfWeek = now.getDay() ; //今天本周的第几天 
    if(nowDayOfWeek == 0) {
      nowDayOfWeek = 6; //周日
    } else {
      nowDayOfWeek = nowDayOfWeek - 1;
    }

    now.setDate(now.getDate() - nowDayOfWeek - 7);
    end.setDate(now.getDate() + 6);
  } else if(v == 5){
    //本月
    now.setDate(1);

    var nextMonth = end.getMonth() + 1;
    var nextMonthFirstDay=new Date(end.getFullYear(), nextMonth,1);
    var oneDay= 1000 * 60 * 60 * 24;
    end = new Date(nextMonthFirstDay-oneDay);
  } else if(v == 6){
    //上月
    var nextMonth = now.getMonth() - 1;
    now.setMonth(nextMonth);
    now.setDate(1);

    nextMonth = nextMonth + 1;
    var nextMonthFirstDay= new Date(end.getFullYear(),nextMonth, 1);
    var oneDay= 1000 * 60 * 60 * 24;
    end = new Date(nextMonthFirstDay-oneDay);
  }

  now.setHours(0);
  now.setMinutes(0);
  now.setSeconds(0);
  end.setHours(23);
  end.setMinutes(59);
  end.setSeconds(59);

  //我们在这里 输出一下
  console.log(now + ‘:‘ + end);
}





























































以上是关于SQL语句获取当天,昨天,本周,上周,本月,上月的起始时间和结束时间的主要内容,如果未能解决你的问题,请参考以下文章

获取今天,昨天,本周,上周,本月,上月时间

JS时间 获取 当天,昨日,本周,上周,本月,上月

Sql Server中查询今天昨天本周上周本月上月数据

Oracle查询今天昨天本周上周本月上月数据

php一行代码获取本周一,本周日,上周一,上周日,本月一日,本月最后一日,上月一日,上月最后一日日期

php一行代码获取本周一,本周日,上周一,上周日,本月一日,本月最后一日,上月一日,上月最后一日日期 转