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


Posted in Javascript onJanuary 04, 2017

话不多说,请看代码:

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

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

Javascript 相关文章推荐
iframe 父窗口和子窗口相互的调用方法集锦
Dec 15 Javascript
javascript编码的几个方法详细介绍
Jan 06 Javascript
探讨jQuery的ajax使用场景(c#)
Dec 03 Javascript
JS动态修改iframe内嵌网页地址的方法
Apr 01 Javascript
javascript中eval和with用法实例总结
Nov 30 Javascript
JavaScript实现复制内容到粘贴板代码
Mar 31 Javascript
jquery利用json实现页面之间传值的实例解析
Dec 12 Javascript
JavaScript定时器制作弹窗小广告
Feb 05 Javascript
详解微信小程序框架wepy踩坑记录(与vue对比)
Mar 12 Javascript
Vue实现图片与文字混输效果
Dec 04 Javascript
Vue 请求传公共参数的操作
Jul 31 Javascript
Vue select 绑定动态变量的实例讲解
Oct 22 Javascript
手机端js和html5刮刮卡效果
Sep 29 #Javascript
JQuery和HTML5 Canvas实现弹幕效果
Jan 04 #Javascript
laydate.js日期时间选择插件
Jan 04 #Javascript
AngularJS使用ng-app自动加载bootstrap框架问题分析
Jan 04 #Javascript
微信小程序 解决swiper不显示图片的方法
Jan 04 #Javascript
bootstrap laydate日期组件使用详解
Jan 04 #Javascript
BootStrap 模态框实现刷新网页并关闭功能
Jan 04 #Javascript
You might like
海河写的 Discuz论坛帖子调用js的php代码
2007/08/23 PHP
Zend Framework教程之Application和Bootstrap用法详解
2016/03/10 PHP
PHP定时任务获取微信access_token的方法
2016/10/10 PHP
JavaScript中void(0)的具体含义解释
2007/02/27 Javascript
JS判断客户端是手机还是PC的2个代码
2014/04/12 Javascript
JavaScript中神奇的call()方法
2015/03/12 Javascript
Kendo Grid editing 自定义验证报错提示的解决方法
2016/11/18 Javascript
纯js实现倒计时功能
2017/01/06 Javascript
Javascript中for循环语句的几种写法总结对比
2017/01/23 Javascript
js图片放大镜实例讲解(必看篇)
2017/07/17 Javascript
详解Vue中watch的详细用法
2018/11/28 Javascript
详解element-ui日期时间选择器的日期格式化问题
2019/04/08 Javascript
JavaScript原生数组函数实例汇总
2020/10/14 Javascript
vue使用Sass时报错问题的解决方法
2020/10/14 Javascript
[49:31]DOTA2-DPC中国联赛 正赛 Elephant vs LBZS BO3 第二场 1月29日
2021/03/11 DOTA
Python实现读取目录所有文件的文件名并保存到txt文件代码
2014/11/22 Python
python根据给定文件返回文件名和扩展名的方法
2015/03/27 Python
由Python运算π的值深入Python中科学计算的实现
2015/04/17 Python
Python中的字典与成员运算符初步探究
2015/10/13 Python
python之virtualenv的简单使用方法(必看篇)
2017/11/25 Python
利用TensorFlow训练简单的二分类神经网络模型的方法
2018/03/05 Python
详解PyCharm+QTDesigner+PyUIC使用教程
2019/06/13 Python
Gauss-Seidel迭代算法的Python实现详解
2019/06/29 Python
Flask教程之重定向与错误处理实例分析
2019/08/01 Python
python之PyQt按钮右键菜单功能的实现代码
2019/08/17 Python
Python提取PDF内容的方法(文本、图像、线条等)
2019/09/25 Python
python3用PyPDF2解析pdf文件,用正则匹配数据方式
2020/05/12 Python
Python检测端口IP字符串是否合法
2020/06/05 Python
安德玛菲律宾官网:Under Armour菲律宾
2020/07/28 全球购物
继电保护工岗位职责
2014/01/05 职场文书
人身损害赔偿协议书范本
2014/09/27 职场文书
群众路线教育实践活动总结
2014/10/30 职场文书
婚育证明样本
2015/06/16 职场文书
【HBU】数据库第四周 单表查询
2021/04/05 SQL Server
vue cli4中mockjs在dev环境和build环境的配置详情
2022/04/06 Vue.js
delete in子查询不走索引问题分析
2022/07/07 MySQL