舉個慄子<script> var now = new Date();// now.setDate(now.getDate()-num);// var result = '';// result+=now.getFullYear()+'-'+// (now.getMonth()+1)+'-'+// ( ...
舉個慄子
<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);
};