欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

获取今天,昨天,本周,上周,本月,上月时间(实例分享)

程序员文章站 2023-12-13 17:57:16
话不多说,请看代码: //获取今天 var nowdate= new date(); //当天日期 console.log(nowdate); //今天是本...

话不多说,请看代码:

//获取今天
var nowdate= new date(); //当天日期
console.log(nowdate);
//今天是本周的第几天
var nowdayofweek= nowdate.getday();
console.log(nowdayofweek);
//当前日
var nowday = nowdate.getdate();
console.log(nowday);
//当前月
var nowmonth = nowdate.getmonth();
console.log(nowmonth);
//当前年
var nowyear = nowdate.getfullyear();
console.log(nowyear);
//var nowhours = nowdate.gethours();
//var nowminutes = nowdate.getminutes();
//var nowseconds = nowdate.getseconds();
nowyear += (nowyear < 2000) ? 1900 : 0; //
console.log(nowyear);
var lastmonthdate = new date(); //上月日期
console.log(lastmonthdate);
lastmonthdate.setdate(1);
console.log(lastmonthdate.setdate(1));
lastmonthdate.setmonth(lastmonthdate.getmonth()-1);
console.log(lastmonthdate.setmonth(lastmonthdate.getmonth()-1));
var lastyear = lastmonthdate.getyear();
console.log(lastyear);
var lastmonth = lastmonthdate.getmonth();
console.log(lastmonth);
//格式化日期:yyyy-mm-dd
function formatdate(date) {
 var myyear = date.getfullyear();
 var mymonth = date.getmonth()+1;
 var myweekday = date.getdate();
 //var myhours = date.gethours();
 //var myminutes = date.getminutes();
 //var myseconds = date.getseconds();
 if(mymonth < 10){
 mymonth = "0" + mymonth;
 }
 if(myweekday < 10){
 myweekday = "0" + myweekday;
 }
 //if(myhours < 10){
 // myhours = "0" + myhours;
 //}
 //if(myminutes < 10){
 // myminutes = "0" + myminutes;
 //}
 return (myyear+"/"+mymonth + "/" + myweekday);
 //return (myyear+"/"+mymonth + "/" + myweekday + " " + myhours+ ":" + myminutes);
}
//获得某月的天数
function getmonthdays(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 getquarterstartmonth(){
// var quarterstartmonth = 0;
// if(nowmonth<3){
// quarterstartmonth = 0;
// }
// if(2<6){
// quarterstartmonth = 3;
// }
// if(5<9){
// quarterstartmonth = 6;
// }
// if(nowmonth>8){
// quarterstartmonth = 9;
// }
// return quarterstartmonth;
//}
//今天
$scope.today = function(){
 var getcurrentdate = new date();
 var getcurrentdate = formatdate(getcurrentdate);
 $scope.today = getcurrentdate;
 console.log($scope.today);
 $("#jquerypickertime3").val($scope.today);
 $("#jquerypickertime4").val($scope.today);
};
//昨天
$scope.yesterday = function(){
 var getyesterdaydate = new date(nowyear, nowmonth, nowday - 1);
 var getyesterdaydate = formatdate(getyesterdaydate);
 $scope.yestday = getyesterdaydate;
 console.log(getyesterdaydate);
 $("#jquerypickertime3").val($scope.yestday);
 $("#jquerypickertime4").val($scope.yestday);
};
//获得本周的开始日期
$scope.thisweek = function(){
 var getweekstartdate = new date(nowyear, nowmonth, nowday - nowdayofweek);
 var getweekstartdate = formatdate(getweekstartdate);
 $scope.tswkstart = getweekstartdate;
 console.log($scope.tswkstart);
 $("#jquerypickertime3").val($scope.tswkstart);
 //获得本周的结束日期
 var getweekenddate = new date(nowyear, nowmonth, nowday + (6 - nowdayofweek));
 var getweekenddate = formatdate(getweekenddate);
 $scope.tswkend = getweekenddate;
 console.log($scope.tswkend);
 $("#jquerypickertime4").val($scope.tswkend);
};
$scope.lastweek = function(){
 //获得上周的开始日期
 var getupweekstartdate = new date(nowyear, nowmonth, nowday - nowdayofweek -7);
 var getupweekstartdate = formatdate(getupweekstartdate);
 $scope.startlastweek = getupweekstartdate;
 console.log($scope.startlastweek);
 $("#jquerypickertime3").val($scope.startlastweek);
 //获得上周的结束日期
 var getupweekenddate = new date(nowyear, nowmonth, nowday + (6 - nowdayofweek - 7));
 var getupweekenddate = formatdate(getupweekenddate);
 $scope.endlastweek = getupweekenddate;
 console.log($scope.endlastweek);
 $("#jquerypickertime4").val($scope.endlastweek);
};
//本月
$scope.thismonth = function(){
 //获得本月的开始日期
 var getmonthstartdate = new date(nowyear, nowmonth, 1);
 var getmonthstartdate = formatdate(getmonthstartdate);
 $scope.startthismonth = getmonthstartdate;
 console.log($scope.startthismonth);
 $("#jquerypickertime3").val($scope.startthismonth);
 //获得本月的结束日期
 var getmonthenddate = new date(nowyear, nowmonth, getmonthdays(nowmonth));
 var getmonthenddate = formatdate(getmonthenddate);
 $scope.endthismonth = getmonthenddate;
 console.log($scope.endthismonth);
 $("#jquerypickertime4").val($scope.endthismonth);
};
//上月
$scope.lastmonth = function(){
 //获得上月开始时间
 var getlastmonthstartdate = new date(nowyear, lastmonth+1, 1);
 var getlastmonthstartdate = formatdate(getlastmonthstartdate);
 $scope.startlastmonth = getlastmonthstartdate;
 console.log($scope.startlastmonth);
 $("#jquerypickertime3").val($scope.startlastmonth);
 //获得上月结束时间
 var getlastmonthenddate = new date(nowyear, lastmonth+1, getmonthdays(lastmonth+1));
 var getlastmonthenddate = formatdate(getlastmonthenddate);
 $scope.endlastmonth = getlastmonthenddate;
 console.log($scope.endlastmonth);
 $("#jquerypickertime4").val($scope.endthismonth);
};

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!

上一篇:

下一篇: