获取今天,昨天,本周,上周,本月,上月时间(实例分享)
发布时间 - 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 】
相关推荐:
jquery插件bootstrapValidator表单验证详解
如何生成腾讯云建站专用兑换码?
Laravel观察者模式如何使用_Laravel Model Observer配置
如何确认建站备案号应放置的具体位置?
如何在Tomcat中配置并部署网站项目?
手机软键盘弹出时影响布局的解决方法
阿里云高弹*务器配置方案|支持分布式架构与多节点部署
Laravel怎么清理缓存_Laravel optimize clear命令详解
如何快速生成高效建站系统源代码?
夸克浏览器网页跳转延迟怎么办 夸克浏览器跳转优化
linux写shell需要注意的问题(必看)
高端云建站费用究竟需要多少预算?
Python文件异常处理策略_健壮性说明【指导】
HTML5建模怎么导出为FBX格式_FBX格式兼容性及导出步骤【指南】
企业网站制作这些问题要关注
长沙做网站要多少钱,长沙国安网络怎么样?
HTML5空格和nbsp有啥关系_nbsp的作用及使用场景【说明】
浅谈Javascript中的Label语句
用v-html解决Vue.js渲染中html标签不被解析的问题
javascript事件捕获机制【深入分析IE和DOM中的事件模型】
ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】
php 三元运算符实例详细介绍
Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】
EditPlus 正则表达式 实战(3)
Android使用GridView实现日历的简单功能
详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
如何快速搭建二级域名独立网站?
详解jQuery中的事件
浏览器如何快速切换搜索引擎_在地址栏使用不同搜索引擎【搜索】
米侠浏览器网页背景异常怎么办 米侠显示修复
如何在IIS中配置站点IP、端口及主机头?
如何在腾讯云服务器上快速搭建个人网站?
深入理解Android中的xmlns:tools属性
JavaScript Ajax实现异步通信
如何注册花生壳免费域名并搭建个人网站?
个人摄影网站制作流程,摄影爱好者都去什么网站?
Laravel如何使用Sanctum进行API认证?(SPA实战)
Python文件操作最佳实践_稳定性说明【指导】
如何在搬瓦工VPS快速搭建网站?
重庆市网站制作公司,重庆招聘网站哪个好?
Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】
怎么制作网站设计模板图片,有电商商品详情页面的免费模板素材网站推荐吗?
如何在景安服务器上快速搭建个人网站?
如何用腾讯建站主机快速创建免费网站?
专业型网站制作公司有哪些,我设计专业的,谁给推荐几个设计师兼职类的网站?
原生JS实现图片轮播切换效果
如何在IIS中新建站点并配置端口与IP地址?
如何快速重置建站主机并恢复默认配置?
,怎么在广州志愿者网站注册?
怎么用AI帮你为初创公司进行市场定位分析?

