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


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 相关文章推荐
event.X和event.clientX的区别分析
Oct 06 Javascript
javascript模块化是什么及其优缺点介绍
Sep 02 Javascript
jquery提交form表单简单示例分享
Mar 03 Javascript
通过JS来动态的修改url,实现对url的增删查改
Sep 01 Javascript
jQuery隐藏和显示效果实现
Apr 06 Javascript
基于javascript数组实现图片轮播
May 02 Javascript
原生js和jquery分别实现横向导航菜单效果
May 13 Javascript
jquery解析XML及获取XML节点名称的实现代码
May 18 Javascript
微信小程序实现锚点定位楼层跳跃的实例
May 18 Javascript
gulp解决跨域的配置文件问题
Jun 08 Javascript
使用Vue做一个简单的todo应用的三种方式的示例代码
Oct 20 Javascript
vue微信分享插件使用方法详解
Feb 18 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
PHP4和PHP5性能测试和对比 测试代码与环境
2007/08/17 PHP
PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法
2014/06/23 PHP
PHP实现生成带背景的图形验证码功能
2016/10/03 PHP
Laravel框架之解决前端显示图片问题
2019/10/24 PHP
解决extjs在firefox中关闭窗口再打开后iframe中js函数访问不到的问题
2008/11/06 Javascript
浅析Prototype的模板类 Template
2011/12/07 Javascript
animate动画示例(泪奔的小孩)及stop和delay的使用
2013/05/06 Javascript
JavaScript:Div层拖动效果实例代码
2013/08/06 Javascript
window.location.href中url中数据量太大时的解决方法
2013/12/23 Javascript
Jquery图片延迟加载插件jquery.lazyload.js的使用方法
2014/05/21 Javascript
JavaScript让Textarea支持tab按键的方法
2015/06/26 Javascript
jquery图片滚动放大代码分享(1)
2015/08/25 Javascript
10个很棒的jQuery代码片段
2015/09/24 Javascript
javascript弹出窗口实现代码
2015/11/12 Javascript
关于AngularJs数据的本地存储详解
2017/01/20 Javascript
Vue+axios 实现http拦截及路由拦截实例
2017/04/25 Javascript
JavaScript实现一个空中避难的小游戏
2017/06/06 Javascript
Node.js readline模块与util模块的使用
2018/03/01 Javascript
基于nodejs的微信JS-SDK简单应用实现
2019/05/21 NodeJs
JavaScript如何处理移动端拍摄图片旋转问题
2019/11/16 Javascript
vue实现全屏滚动效果(非fullpage.js)
2020/03/07 Javascript
基于js实现判断浏览器类型代码实例
2020/07/17 Javascript
Python中使用Boolean操作符做真值测试实例
2015/01/30 Python
python使用xlrd模块读写Excel文件的方法
2015/05/06 Python
Python实现的选择排序算法原理与用法实例分析
2017/11/22 Python
cmd运行python文件时对结果进行保存的方法
2018/05/16 Python
Python 最大概率法进行汉语切分的方法
2018/12/14 Python
python使用knn实现特征向量分类
2018/12/26 Python
python 处理数字,把大于上限的数字置零实现方法
2019/01/28 Python
python实现植物大战僵尸游戏实例代码
2019/06/10 Python
Python3 读取Word文件方式
2020/02/13 Python
pytorch 多分类问题,计算百分比操作
2020/07/09 Python
详解HTML5中的标签
2015/06/19 HTML / CSS
2015年南京大屠杀纪念日活动总结
2015/03/24 职场文书
2015年司机工作总结
2015/04/23 职场文书
2015年监理个人工作总结
2015/05/23 职场文书