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

发布时间 - 2026-01-10 22:17:54    点击率:

话不多说,请看代码:

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


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: java中使用zxing批量生成二维码立牌  如何快速生成橙子建站落地页链接?  HTML 中如何正确使用模板变量为元素的 name 属性赋值  Midjourney怎样加参数调细节_Midjourney参数调整技巧【指南】  Laravel怎么进行浏览器测试_Laravel Dusk自动化浏览器测试入门  再谈Python中的字符串与字符编码(推荐)  利用python获取某年中每个月的第一天和最后一天  清除minerd进程的简单方法  Laravel怎么配置S3云存储驱动_Laravel集成阿里云OSS或AWS S3存储桶【教程】  软银砸40亿美元收购DigitalBridge 强化AI资料中心布局  JavaScript中如何操作剪贴板_ClipboardAPI怎么用  jQuery中的100个技巧汇总  node.js报错:Cannot find module &#39;ejs&#39;的解决办法  edge浏览器无法安装扩展 edge浏览器插件安装失败【解决方法】  百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏  Laravel如何使用Blade组件和插槽?(Component代码示例)  Laravel怎么配置不同环境的数据库_Laravel本地测试与生产环境动态切换【方法】  Win11怎么关闭资讯和兴趣_Windows11任务栏设置隐藏小组件  Laravel如何生成API文档?(Swagger/OpenAPI教程)  在线制作视频的网站有哪些,电脑如何制作视频短片?  Laravel怎么调用外部API_Laravel Http Client客户端使用  三星网站视频制作教程下载,三星w23网页如何全屏?  网站页面设计需要考虑到这些问题  EditPlus中的正则表达式实战(6)  香港服务器建站指南:免备案优势与SEO优化技巧全解析  Laravel怎么判断请求类型_Laravel Request isMethod用法  做企业网站制作流程,企业网站制作基本流程有哪些?  原生JS实现图片轮播切换效果  如何在七牛云存储上搭建网站并设置自定义域名?  Laravel怎么写单元测试_PHPUnit在Laravel项目中的基础测试入门  Laravel路由怎么定义_Laravel核心路由系统完全入门指南  Laravel怎么创建控制器Controller_Laravel路由绑定与控制器逻辑编写【指南】  HTML5建模怎么导出为FBX格式_FBX格式兼容性及导出步骤【指南】  如何在阿里云香港服务器快速搭建网站?  湖南网站制作公司,湖南上善若水科技有限公司做什么的?  JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)  Android自定义listview布局实现上拉加载下拉刷新功能  JavaScript Ajax实现异步通信  如何快速打造个性化非模板自助建站?  Laravel Sail是什么_基于Docker的Laravel本地开发环境Sail入门  Laravel如何使用Service Provider注册服务_Laravel服务提供者配置与加载  Laravel如何获取当前用户信息_Laravel Auth门面获取用户ID  JS弹性运动实现方法分析  如何挑选优质建站一级代理提升网站排名?  C#如何调用原生C++ COM对象详解  如何彻底删除建站之星生成的Banner?  Laravel如何与Vue.js集成_Laravel + Vue前后端分离项目搭建指南  ,怎么在广州志愿者网站注册?  怎么用AI帮你设计一套个性化的手机App图标?  html文件怎么打开证书错误_https协议的html打开提示不安全【指南】