js实现日期显示的一些操作(实例讲解)
程序员文章站
2022-07-05 20:49:45
1、js获取当前日期(yyyy-mm-dd)
以下代码是获取到的当前日期:
var mydate = new date();
var year = myda...
1、js获取当前日期(yyyy-mm-dd)
以下代码是获取到的当前日期:
var mydate = new date(); var year = mydate.getfullyear(); //获取完整的年份(4位,1970-????) var month = mydate.getmonth()+1; //获取当前月份(1-12) var day = mydate.getdate(); //获取当前日(1-31) //获取完整年月日 var newday = year + “-” + month + “-” + day;
2、点击实现日期的天数加减(yyyy-mm-dd)
点击俩个按钮分别可以实现日期的加减,如果本月天数达到最多,那么月份将会自动增加或减少
var n = 0; daychange(0) $("#time-add").click(function(){ n++; daychange(n); }) $("#time-less").click(function(){ n--; daychange(n); }) function daychange(n){ var now = new date();//今天 var tomo = new date((now/1000+86400*n)*1000);//明天 var month = tomo.getmonth() + 1; var strdate = tomo.getdate(); var seperator1 = "-"; if (month >= 1 && month <= 9) { month = "0" + month; } if (strdate >= 0 && strdate <= 9) { strdate = "0" + strdate; } var currentdate = tomo.getfullyear() + seperator1 + month + seperator1 + strdate; $(".center-day").html(currentdate); }
3、获取当前本周周一和本周周日的时间范围
不管当前是周几,都可以获取到当前所在这一周的起始时间
var now = new date();//今天 week(now); function week(now){ var nowtime = now.gettime() ; var day = now.getday(); var onedaylong = 24*60*60*1000 ; //获取本周所在周一 var mondaytime = nowtime - (day-1)*onedaylong ; //获取本周所在周末 var sundaytime = nowtime + (7-day)*onedaylong ; //转化日期 var monday = new date(mondaytime); var sunday = new date(sundaytime); var month = monday.getmonth() + 1; var strdate = monday.getdate(); var month1 = sunday.getmonth() + 1; var strdate1 = sunday.getdate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (month1 >= 1 && month1 <= 9) { month1 = "0" + month1; } if (strdate >= 0 && strdate <= 9) { strdate = "0" + strdate; } if (strdate1 >= 0 && strdate1 <= 9) { strdate1 = "0" + strdate1; } currentdate = monday.getfullyear() + seperator1 + month + seperator1 + strdate + "至" + sunday.getfullyear() + seperator1 + month1 + seperator1 + strdate1; $(".center-day").html(currentdate); 32}
4、点击实现每周范围的变化
点击改变的按钮将会改变显示一周范围的改变,如果有的在下一月或者下一年,那么将会自动显示,不会出现错误
var now = new date();//今天 var n = 0; week(now); $("#week-add").click(function(){ n++; var date = new date(now.gettime() + n*7*24*3600*1000); week(date); }) $("#week-add").click(function(){ n--; var date = new date(now.gettime() + n*7*24*3600*1000); week(date); }) function week(now){ var nowtime = now.gettime() ; var day = now.getday(); var onedaylong = 24*60*60*1000 ; //获取本周所在周一 var mondaytime = nowtime - (day-1)*onedaylong ; //获取本周所在周末 var sundaytime = nowtime + (7-day)*onedaylong ; 22//转化日期 var monday = new date(mondaytime); var sunday = new date(sundaytime); var month = monday.getmonth() + 1; var strdate = monday.getdate(); var month1 = sunday.getmonth() + 1; var strdate1 = sunday.getdate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (month1 >= 1 && month1 <= 9) { month1 = "0" + month1; } if (strdate >= 0 && strdate <= 9) { strdate = "0" + strdate; } if (strdate1 >= 0 && strdate1 <= 9) { strdate1 = "0" + strdate1; } currentdate = monday.getfullyear() + seperator1 + month + seperator1 + strdate + "至" + sunday.getfullyear() + seperator1 + month1 + seperator1 + strdate1; $(".center-day").html(currentdate); }
5、获取当前月份的第一天和最后一天
能够获取到当前所在月份的第一天和最后一天,最后一天的日期是不固定的,能够获取到应有的日期
monthfen(0) function monthfen(n){ var now = new date();//今天 var firstdate = new date((now/1000+86400*n*now.getdate())*1000);//明天 //本月第一天 firstdate.setdate(1); //第一天 var date = new date(firstdate); var month = date.getmonth() + 1; var strdate = "0" + date.getdate(); //本月最后一天 var enddate = new date(firstdate); enddate.setmonth(firstdate.getmonth()+1); enddate.setdate(0); var date1 = new date(enddate); var month1 = date1.getmonth() + 1; var strdate1 = date1.getdate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (month1 >= 1 && month1 <= 9) { month1 = "0" + month1; } currentdate = date.getfullyear() + seperator1 + month + seperator1 + strdate + "至" + date1.getfullyear() + seperator1 + month1 + seperator1 + strdate1; $(".center-day").html(currentdate); }
6、点击实现当前月份的改变
点击按钮会实现当前月份的改变,那么最后一天的日期也会自动改变,
monthfen(0) var n = 0; $("#month-add").click(function(){ n++; monthfen(n); }) $("#month-less").click(function(){ n--; monthfen(n); }) function monthfen(n){ var now = new date();//今天 var firstdate = new date((now/1000+86400*n*now.getdate())*1000);//明天 //本月第一天 firstdate.setdate(1); //第一天 var date = new date(firstdate); var month = date.getmonth() + 1; var strdate = "0" + date.getdate(); //本月最后一天 var enddate = new date(firstdate); enddate.setmonth(firstdate.getmonth()+1); enddate.setdate(0); var date1 = new date(enddate); var month1 = date1.getmonth() + 1; var strdate1 = date1.getdate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (month1 >= 1 && month1 <= 9) { month1 = "0" + month1; } currentdate = date.getfullyear() + seperator1 + month + seperator1 + strdate + "至" + date1.getfullyear() + seperator1 + month1 + seperator1 + strdate1; $(".center-day").html(currentdate); }
当然还有很多关于日期格式的改变和算法,如果有什么不理解的可以留下评论,大家一起探讨。
以上这篇js实现日期显示的一些操作(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。