快捷选时间

举个栗子
<script>
    var now = new Date();
// now.setDate(now.getDate()-num);
// var result = '';
// result+=now.getFullYear()+'-'+
// (now.getMonth()+1)+'-'+
// (now.getDate());
//格式化日期:yyyy-MM-dd
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 toDay(){
var now = new Date();
return "今天:"+formatDate(now);
}
console.log(toDay());
// 昨天
function yesTerDay(){
var now = new Date();
now.setDate(now.getDate()-1);
return "昨天:"+formatDate(now);
}
console.log(yesTerDay());
// 本周
function thisWeek(){
var now = new Date();
var nowWeek = now.getDay();// 当前星期
var getWeekEndDate = formatDate(now); // 本周结束日期
now.setDate(now.getDate()-nowWeek);
var getWeekStartDate = formatDate(now); // 本周开始日期
return "本周开始时间:"+getWeekStartDate+" 本周结束时间:"+getWeekEndDate;
}
console.log(thisWeek());
// 上周
function lastWeek(){
var now = new Date();
var nowWeek = now.getDay();
now.setDate(now.getDate()-nowWeek-7);
var getUpWeekStartDate = formatDate(now); // 上周开始日期
now.setDate(now.getDate()+6);
var getUpWeekEndDate = formatDate(now); // 上周结束日期
return "上周开始时间:"+getUpWeekStartDate+" 上周结束时间:"+getUpWeekEndDate;
}
console.log(lastWeek());
// 本月
function thisMonth(){
var now = new Date();
var nowDate = now.getDate();//当前日期
var getMonthEndDate = formatDate(now); // 本月结束日期
now.setDate(now.getDate()-nowDate+1);
var getMonthStartDate = formatDate(now); // 本月开始日期
return "本月开始时间:"+getMonthStartDate+" 本月结束时间:"+getMonthEndDate;
}
console.log(thisMonth());
// 上月
function lastMonth(){
var now = new Date();
var nowDate = now.getDate();//当前日期
now.setDate(now.getDate()-nowDate+1);
now.setMonth(now.getMonth() - 1);
var getLastMonthStartDate = formatDate(now); // 上月开始日期
now.setMonth(now.getMonth() +1);
now.setDate(now.getDate()-1);
var getLastMonthEndDate = formatDate(now); // 上月结束日期
return "上月开始时间:"+getLastMonthStartDate+" 上月结束时间:"+getLastMonthEndDate;
}
console.log(lastMonth());

</script>

在angular中
//格式化日期:yyyy-MM-dd
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);
}

var now = new Date();
$scope.ToDay = formatDate(now);

//今天
$scope.toDay = function(){
var now = new Date();
$scope.today = formatDate(now);

$(".recordTime1").val($scope.today);
$(".recordTime2").val($scope.today);
};

//昨天
$scope.yesTerDay = function(){
var now = new Date();
now.setDate(now.getDate()-1);
$scope.yesTday = formatDate(now);

$(".recordTime1").val($scope.yesTday);
$(".recordTime2").val($scope.yesTday);
};


//本周日期
$scope.thisWeek = function(){
var now = new Date();
var nowWeek = now.getDay();// 当前星期
$scope.tswkEnd = formatDate(now); // 本周结束日期
now.setDate(now.getDate()-nowWeek);
$scope.tswkStart = formatDate(now); // 本周开始日期

$(".recordTime1").val($scope.tswkStart);
$(".recordTime2").val($scope.tswkEnd);
};
//上周
$scope.lastWeek = function(){
var now = new Date();
var nowWeek = now.getDay();
now.setDate(now.getDate()-nowWeek-7);
$scope.startLastWeek = formatDate(now); // 上周开始日期
now.setDate(now.getDate()+6);
$scope.endLastWeek = formatDate(now); // 上周结束日期
$(".recordTime1").val($scope.startLastWeek);
$(".recordTime2").val($scope.endLastWeek);

};
//本月
$scope.thisMonth = function(){

var now = new Date();
var nowDate = now.getDate();//当前日期
$scope.endThisMonth= formatDate(now); // 本月结束日期
now.setDate(now.getDate()-nowDate+1);
$scope.startThisMonth = formatDate(now); // 本月开始日期

$(".recordTime1").val($scope.startThisMonth);
$(".recordTime2").val($scope.endThisMonth);
};

//上月
$scope.lastMonth = function(){
var now = new Date();
var nowDate = now.getDate();//当前日期
now.setDate(now.getDate()-nowDate+1);
now.setMonth(now.getMonth() - 1);
$scope.startLastMonth = formatDate(now); // 上月开始日期

now.setMonth(now.getMonth() +1);
now.setDate(now.getDate()-1);
$scope.endLastMonth = formatDate(now); // 上月结束日期

$(".recordTime1").val($scope.startLastMonth);
$(".recordTime2").val($scope.endLastMonth);
};
原文地址:https://www.cnblogs.com/liuhongjin/p/6249256.html