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

/*处理时间戳*/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 = new Date(timeStamp);    Date.prototype.format = function (format) {        var date = {            "M+": this.getMonth() + 1,            "d+": this.getDate(),            "h+": this.getHours(),            "m+": this.getMinutes(),            "s+": this.getSeconds(),            "q+": Math.floor((this.getMonth() + 3) / 3),            "S+": this.getMilliseconds()        };        if (/(y+)/i.test(format)) {            format = format.replace(RegExp.$1, (this.getFullYear() + ‘‘).substr(4 - RegExp.$1.length));        }        for (var k in date) {            if (new RegExp("(" + k + ")").test(format)) {                format = format.replace(RegExp.$1, RegExp.$1.length == 1                    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));            }        }        return format;    };    var time = newDate.format(‘yyyy-MM-dd hh:mm:ss‘);//查找时间点,转换成时间类型    return time;};/*获取当天日期*/$scope.getCurrentDay=function () {    var tomorrowTime = new Date(new Date().getTime() +24*60*60*1000);    ctrl.params = {        startTime: $filter(‘date‘)(new Date(), ‘yyyy-MM-dd‘),        endTime: tomorrowTime    };};/*获取昨天日期*/$scope.getYesterdayDay=function () {    var yesterdayTime = new Date(new Date().getTime() -24*60*60*1000);    var getYesterdayDate =  formatDate(yesterdayTime);    ctrl.params = {        startTime: getYesterdayDate,        endTime: $filter(‘date‘)(new Date(), ‘yyyy-MM-dd‘)    };};/*获取本周日期*/$scope.getThisWeek=function () {    var now = new Date();    var nowDayOfWeek = now.getDay(); // 今天本周的第几天    var nowDay = now.getDate(); // 当前日    var nowMonth = now.getMonth(); // 当前月    var nowYear = now.getYear(); // 当前年    nowYear += (nowYear < 2000) ? 1900 : 0; //    var lastMonthDate = new Date(); // 上月日期    lastMonthDate.setDate(1);    lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);    var lastYear = lastMonthDate.getYear();    var lastMonth = lastMonthDate.getMonth();    // 获得上周的开始日期    var getUpWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek+1);    getUpWeekStartDate = formatDate(getUpWeekStartDate);    // 获得上周的结束日期    var getUpWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek + 1));    getUpWeekEndDate = formatDate(getUpWeekEndDate);    ctrl.params = {        startTime: $filter(‘date‘)(getUpWeekStartDate, ‘yyyy-MM-dd‘),        endTime: $filter(‘date‘)(getUpWeekEndDate, ‘yyyy-MM-dd‘)    };    //console.log(ctrl.params);};/*获取上周日期*/$scope.getPreviousWeek=function () {    var now = new Date();    var nowDayOfWeek = now.getDay(); // 今天本周的第几天    var nowDay = now.getDate(); // 当前日    var nowMonth = now.getMonth(); // 当前月    var nowYear = now.getYear(); // 当前年    nowYear += (nowYear < 2000) ? 1900 : 0; //    var lastMonthDate = new Date(); // 上月日期    lastMonthDate.setDate(1);    lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);    var lastYear = lastMonthDate.getYear();    var lastMonth = lastMonthDate.getMonth();    // 获得上周的开始日期    var getUpWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 6);    getUpWeekStartDate = formatDate(getUpWeekStartDate);    // 获得上周的结束日期    var getUpWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek - 6));    getUpWeekEndDate = formatDate(getUpWeekEndDate);    ctrl.params = {        startTime: $filter(‘date‘)(getUpWeekStartDate, ‘yyyy-MM-dd‘),        endTime: $filter(‘date‘)(getUpWeekEndDate, ‘yyyy-MM-dd‘)    };    //console.log(ctrl.params);};/*获取本月日期*/$scope.getThisMonth=function () {    ctrl.params = {        startTime: $filter(‘date‘)(new Date().setDate(1), ‘yyyy-MM-dd‘),        endTime: $filter(‘date‘)(new Date(), ‘yyyy-MM-dd‘)    };};/*获取上月日期*/$scope.getPreviousMonth=function () {    var lastMonthDate = new Date(); //上月日期    lastMonthDate.setDate(1);    lastMonthDate.setMonth(lastMonthDate.getMonth()-1);    var lastMonth = lastMonthDate.getMonth();    var nowYear=timeChange(lastMonthDate.setDate(1)).substr(0,4);    var LastMonthStartDate=getLastMonthStartDate(nowYear,lastMonth);    var LastMonthEndDate=getLastMonthEndDate(nowYear,lastMonth);    ctrl.params = {        startTime: $filter(‘date‘)(LastMonthStartDate, ‘yyyy-MM-dd‘),        endTime: $filter(‘date‘)(LastMonthEndDate, ‘yyyy-MM-dd‘)    };};/*获取今年日期*/$scope.getThisYear=function () {    var thisYearStartDate=new Date().getFullYear()+"-01-01";    var thisYearEndDate=new Date().getFullYear()+"-12-31";    ctrl.params = {        startTime: $filter(‘date‘)(thisYearStartDate, ‘yyyy-MM-dd‘),        endTime: $filter(‘date‘)(thisYearEndDate, ‘yyyy-MM-dd‘)    };};/*获取去年日期*/$scope.getLastYear=function () {    var lastYearStartDate=(new Date().getFullYear()-1)+"-01-01";    var lastYearEndDate=(new Date().getFullYear()-1)+"-12-31";    ctrl.params = {        startTime: $filter(‘date‘)(lastYearStartDate, ‘yyyy-MM-dd‘),        endTime: $filter(‘date‘)(lastYearEndDate, ‘yyyy-MM-dd‘)    };};//获得上月开始时间function getLastMonthStartDate(nowYear,lastMonth){    var lastMonthStartDate = new Date(nowYear, lastMonth, 1);    return formatDate(lastMonthStartDate);}//获得上月结束时间function getLastMonthEndDate(nowYear, lastMonth){    var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(nowYear,lastMonth));    return formatDate(lastMonthEndDate);}/*日期转换*/function formatDate(date) {    var myyear = date.getFullYear();    var mymonth = date.getMonth() + 1;    var myweekday = date.getDate();

if (mymonth < 10) {        mymonth = "0" + mymonth;    }    if (myweekday < 10) {        myweekday = "0" + myweekday;    }    return (myyear + "-" + mymonth + "-" + myweekday);}/*获得某月的天数*/function getMonthDays(nowYear, myMonth) {    var monthStartDate = new Date(nowYear, myMonth, 1);    var monthEndDate = new Date(nowYear, myMonth + 1, 1);    var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);    return days;}/*显示千分位*/function addThousands(number) {    if(number==0){        return number;    }else if(number==0.0){        return number;    }    var num = number + "";    num = num.replace(new RegExp(",","g"),"");    // 正负号处理    var symble = "";    if(/^([-+]).*$/.test(num)) {        symble = num.replace(/^([-+]).*$/,"$1");        num = num.replace(/^([-+])(.*)$/,"$2");    }    if(/^[0-9]+(\.[0-9]+)?$/.test(num)) {        var num = num.replace(new RegExp("^[0]+","g"),"");        if(/^\./.test(num)) {            num = "0" + num;        }        var decimal = num.replace(/^[0-9]+(\.[0-9]+)?$/,"$1");        var integer= num.replace(/^([0-9]+)(\.[0-9]+)?$/,"$1");        var re=/(\d+)(\d{3})/;        while(re.test(integer)){            integer = integer.replace(re,"$1,$2");        }        return symble + integer + decimal;

} else {        return number;    }}Date.prototype.Format = function (fmt) { //author: meizz    var o = {        "M+": this.getMonth() + 1, //月份        "d+": this.getDate(), //日        "h+": this.getHours(), //小时        "m+": this.getMinutes(), //分        "s+": this.getSeconds(), //秒        "q+": Math.floor((this.getMonth() + 3) / 3), //季度        "S": this.getMilliseconds() //毫秒    };    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));    for (var k in o)        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));    return fmt;}
时间: 2024-10-13 17:53:02

获取昨天/今天/本周/下周/本月/下月/去年/今年时间,默认设置时间的相关文章

MSSQL获取昨天,本周,本月。。。

特别说明下:以下统计本周数据时,星期天是作为下周的第一天,而不是本周最后一天,因此你把星期天作为本周最后一天时,你需要在getDate()的基础上减一天,如dateadd('day', -1, getDate()) 本周:select * from table where datediff(week,C_CALLTIME,getdate())=0 --C_CALLTIME 为日期字段 本月:select * from table where datediff(Month,C_CALLTIME,g

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;       

PHP计算今天、昨天、本周、本月、上月开始时间和结束时间

PHP计算今天.昨天.本周.本月.上月开始时间和结束时间 $today = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d'),date('Y'))); $today_end = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1); echo $today,'<br>';//今天开始时间 echo $today_end,'<br>';//今天结束时

java 获取今天、昨天、本周、本月、本年的时间范围的工具类

import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.List; /** * 时间范围工具类 * * @author zwq * */ public class TimeFrameUtil { /** * 获取今天的时间范围 * @return 返回长度为2的字符串集合,如:[2017-11-03 00:00:00, 2017-11-03

mysql查询今天、昨天、本周、本月、上一月 、今年数据

mysql数据库中的关于查询日期的一些操作如下: --今天 select * from 表名 where to_days(时间字段名) = to_days(now()); --昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1 --本周 SELECT * FROM 表名 WHERE YEARWEEK( date_format( 时间字段名,'%Y-%m-%d' ) ) = YEARWEEK( now() ) ; -

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常用时间戳获取 昨天,明天,上周,本月

//昨天0时时间戳$yesterday_zero = strtotime(date('Y-m-d')) - 3600 * 24;//昨天此时时间戳$yesterday_now = strtotime('-1 day');//本周一时间戳$week_this_monday = strtotime('last Monday');//明天时间戳$tomorrow = strtotime("+1 day");//上周一时间戳$week_last_monday = strtotime('last

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

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