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


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 相关文章推荐
JS模拟面向对象全解(二、类型与赋值)
Jul 13 Javascript
自定义右键属性覆盖浏览器默认右键行为实现代码
Feb 02 Javascript
js怎么覆盖原有方法实现重写
Sep 04 Javascript
浅谈javascript 归并方法
Jan 21 Javascript
JavaScript对Json的增删改属性详解
Jun 02 Javascript
D3.js实现饼状图的方法详解
Sep 21 Javascript
微信公众号 客服接口的开发实例详解
Sep 28 Javascript
jQuery网页定位导航特效实现方法
Dec 19 Javascript
详解angularjs结合pagination插件实现分页功能
Feb 10 Javascript
vue router 源码概览案例分析
Oct 09 Javascript
在vue中把含有html标签转为html渲染页面的实例
Oct 28 Javascript
Vue + Node.js + MongoDB图片上传组件实现图片预览和删除功能详解
Apr 29 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
基于数据库的在线人数,日访问量等统计
2006/10/09 PHP
PHP 操作文件的一些FAQ总结
2009/02/12 PHP
php二维数组排序方法(array_multisort usort)
2013/12/25 PHP
54个提高PHP程序运行效率的方法
2015/07/19 PHP
php微信公众平台开发之微信群发信息
2016/09/13 PHP
cakephp常见知识点汇总
2017/02/24 PHP
基于PHP实现邮箱验证激活过程详解
2020/10/28 PHP
jquery插件制作简单示例说明
2012/02/03 Javascript
图片动画横条广告带上下滚动可自定义图片、链接等等
2013/10/20 Javascript
推荐JavaScript实现继承的最佳方式
2014/11/11 Javascript
javascript实现Table间隔色以及选择高亮(和动态切换数据)的方法
2015/05/14 Javascript
jquery序列化方法实例分析
2015/06/10 Javascript
javascript的BOM
2016/05/03 Javascript
最全面的百度地图JavaScript离线版开发
2016/09/10 Javascript
jquery遍历标签中自定义的属性方法
2016/09/17 Javascript
AngularJS指令与指令之间的交互功能示例
2016/12/14 Javascript
详解Jquery的事件操作和文档操作
2016/12/19 Javascript
JS实现数组去重复值的方法示例
2017/02/18 Javascript
Angular.js跨controller实现参数传递的两种方法
2017/02/20 Javascript
微信小程序开发animation心跳动画效果
2017/08/16 Javascript
webpack3+React 的配置全解
2017/08/21 Javascript
AngularJS创建一个上传照片的指令实例代码
2018/02/24 Javascript
vue flex 布局实现div均分自动换行的示例代码
2020/08/05 Javascript
python snownlp情感分析简易demo(分享)
2017/06/04 Python
python3实现猜数字游戏
2020/12/07 Python
Python3.7基于hashlib和Crypto实现加签验签功能(实例代码)
2019/12/04 Python
HTML5的结构和语义(2):结构
2008/10/17 HTML / CSS
法国低价在线宠物商店:bitiba.fr
2020/07/03 全球购物
Java提供了哪些企业应用编程接口
2015/02/13 面试题
学生发电厂实习自我鉴定
2013/09/22 职场文书
毕业生自荐书
2013/12/18 职场文书
教你打造完美的创业计划书
2014/01/06 职场文书
数学系毕业生的自我评价
2014/01/10 职场文书
聚会通知怎么写
2015/04/23 职场文书
乡镇科协工作总结2015
2015/05/19 职场文书
《我和小伙伴》教学反思
2016/02/20 职场文书