全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

话不多说,请看代码:

//获取今天
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);
};

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


# 今天  # 昨天  # 本周  # 上周  # 本月  # 上月  # 时间  # js获取当前时间(昨天、今天、明天)  # 使用javascript将时间转换成今天  # 前天等格式  # js获取日期:昨天今天和明天、后天  # js获取时间(本周、本季度、本月..)  # javascript显示上周、上个月日期的处理方法  # js日期插件dateHelp获取本月、三个月、今年的日期  # js实现获取当前时间是本月第几周的方法  # JavaScript 计算当天是本年本月的第几周  # 显示今天的日期js代码(阳历和农历)  # 今天是星期几的4种JS代码写法  # js判断选择的时间是否大于今天的代码  # 几天  # 前日  # 多说  # 前年  # 当天  # 结束时间  # lastMonthDate  # lt  # getSeconds  # getMinutes  # nowSeconds  # date  # lastYear  # setMonth  # getYear  # yyyy 


相关文章: 如何在IIS中配置站点IP、端口及主机头?  手机网站制作与建设方案,手机网站如何建设?  青浦网站制作公司有哪些,苹果官网发货地是哪里?  如何通过远程VPS快速搭建个人网站?  jQuery 常见小例汇总  教学网站制作软件,学习*后期制作的网站有哪些?  官网自助建站系统:SEO优化+多语言支持,快速搭建专业网站  成都网站制作价格表,现在成都广电的单独网络宽带有多少的,资费是什么情况呢?  海南网站制作公司有哪些,海口网是哪家的?  网页设计与网站制作内容,怎样注册网站?  定制建站如何定义?其核心优势是什么?  建站之星如何保障用户数据免受黑客入侵?  常州自助建站工具推荐:低成本搭建与模板选择技巧  湖州网站制作公司有哪些,浙江中蓝新能源公司官网?  台州网站建设制作公司,浙江手机无犯罪记录证明怎么开?  零服务器AI建站解决方案:快速部署与云端平台低成本实践  建站主机解析:虚拟主机配置与服务器选择指南  制作网站的过程怎么写,用凡科建站如何制作自己的网站?  如何高效配置香港服务器实现快速建站?  如何通过服务器快速搭建网站?完整步骤解析  建站之星免费版是否永久可用?  设计网站制作公司有哪些,制作网页教程?  建站之星后台搭建步骤解析:模板选择与产品管理实操指南  c# await 一个已经完成的Task会发生什么  网站制作话术技巧,网站推广做的好怎么话术?  弹幕视频网站制作教程下载,弹幕视频网站是什么意思?  香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化  昆明高端网站制作公司,昆明公租房申请网上登录入口?  创业网站制作流程,创业网站可靠吗?  公司网站的制作公司,企业网站制作基本流程有哪些?  建站168自助建站系统:快速模板定制与SEO优化指南  用v-html解决Vue.js渲染中html标签不被解析的问题  如何制作算命网站,怎么注册算命网站?  建站主机如何安装配置?新手必看操作指南  如何用手机制作网站和网页,手机移动端的网站能制作成中英双语的吗?  制作网站的公司有哪些,做一个公司网站要多少钱?  三星网站视频制作教程下载,三星w23网页如何全屏?  ui设计制作网站有哪些,手机UI设计网址吗?  网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?  贸易公司网站制作流程,出口贸易网站设计怎么做?  枣阳网站制作,阳新火车站打的到仙岛湖多少钱?  网站制作大概多少钱一个,做一个平台网站大概多少钱?  如何高效完成独享虚拟主机建站?  如何通过云梦建站系统实现SEO快速优化?  网站制作大概要多少钱一个,做一个平台网站大概多少钱?  C#怎么创建控制台应用 C# Console App项目创建方法  可靠的网站设计制作软件,做网站设计需要什么样的电脑配置?  香港网站服务器数量如何影响SEO优化效果?  英语简历制作免费网站推荐,如何将简历翻译成英文?  教育培训网站制作流程,请问edu教育网站的域名怎么申请? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。