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);
}

时间: 2024-11-03 03:46:46

JS时间 获取 当天,昨日,本周,上周,本月,上月的相关文章

Java获取当天、本周、本月 开始及结束时间

1.代码 package com.yong.util; import java.util.Calendar; import java.util.Date; public class TestDate { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("当前时间:"+ new Date().toLocaleString()); System.ou

javascript获取当天、本周、本月、本年 开始及结束时间

/** * 获取本周.本季度.本月.上月的开始日期.结束日期 */ var now = new Date(); //当前日期 var nowDayOfWeek = now.getDay(); //今天本周的第几天 var nowDay = now.getDate(); //当前日 var nowMonth = now.getMonth(); //当前月 var nowYear = now.getYear(); //当前年 nowYear += (nowYear < 2000) ? 1900 :

用php获取本周,上周,本月,上月,本季度日期的代码

1 echo date("Ymd",strtotime("now")), "\n"; 2 echo date("Ymd",strtotime("-1 week Monday")), "\n"; 3 echo date("Ymd",strtotime("-1 week Sunday")), "\n"; 4 echo date(&q

php 获取今天,本周,本月,三个月内,半年内,今年的开始和结束时间

$now = time();         //今天        $today_audit_num = 0;        $today_use_num = 0;        $beginTime = date('Y-m-d 00:00:00', $now);          $endTime = date('Y-m-d 23:59:59', $now); //本周        $week_audit_num = 0;        $week_use_num = 0;       

Sql 查询当天、本周、本月记录

Sql 查询当天.本周.本月记录--查询当天: [sql] view plaincopyprint?select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: [sql] view plaincopyprint?select * from info where DateDiff(hh,datetime,getDate())<=24 --info为表名,datetime为数据库中的字段值 --查询当天:[sql] v

获取昨天/今天/本周/下周/本月/下月/去年/今年时间,默认设置时间

/*处理时间戳*/var getTimeStamp=function(dateStr){ var newstr = dateStr.replace(/-/g,'/'); var date = new Date(newstr); var time_str = date.getTime().toString(); return time_str.substr(0, 13);};/*时间戳转换成正常时间*/var timeChange=function(timeStamp) { var newDate

thinkphp 查询当天 ,本周,本月,本季度,本年度,全部, 数据方法

数据库字段是createtime 里面保存的是时间戳 <?php /* *按今天,本周,本月,本季度,本年,全部查询预约单数据 * $day 代表查询条件 $cid 代表 公司id *返回array $data 查询条件 数组 */ class ReserveModel extends BaseModel { public function find_createtime($day,$cid){ //查询当天数据 if($day==1){ $today=strtotime(date('Y-m-d

Sql 查询当天、本周、本月记录、上周、上月记录

查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 查询24小时内: select * from info where DateDiff(hh,datetime,getDate())<=24 查询当天: select * from table where DateDiff(dd,datetime,getdate())=0 本月记录 : SELECT * FROM 表 WHERE datediff(month,[date

js中获取当天的时间的年月日

var d=new Date() var day=d.getDate() var month=d.getMonth() + 1 var year=d.getFullYear() document.write(day + "." + month + "." + year) document.write("<br /><br />") document.write(year + "/" + month +