首页 > 代码库 > 获取昨天/今天/本周/下周/本月/下月/去年/今年时间,默认设置时间
获取昨天/今天/本周/下周/本月/下月/去年/今年时间,默认设置时间
/*处理时间戳*/
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;
}
获取昨天/今天/本周/下周/本月/下月/去年/今年时间,默认设置时间
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。