1、js获取当前日期(yyyy-mm-dd)

以下代码是获取到的当前日期:

1 var myDate = new Date();
2 var year = myDate.getFullYear();    //获取完整的年份(4位,1970-????)
3 var month = myDate.getMonth() 1;       //获取当前月份(1-12)
4 var day = myDate.getDate();        //获取当前日(1-31)
5 //获取完整年月日
6 var newDay = year   “-”   month   “-”   day;

2、点击实现日期的天数加减(yyyy-mm-dd)

点击俩个按钮分别可以实现日期的加减,如果本月天数达到最多,那么月份将会自动增加或减少

 1 var n = 0;
 2 dayChange(0)
 3 $("#time-add").click(function(){
 4    n  ;
 5    dayChange(n);
 6 })
 7 $("#time-less").click(function(){
 8    n--;
 9    dayChange(n);
10 })
11 function dayChange(n){
12    var now = new Date();//今天
13    var tomo = new Date((now/1000 86400*n)*1000);//明天
14    var month = tomo.getMonth()   1;
15    var strDate = tomo.getDate();
16    var seperator1 = "-";
17    if (month >= 1 && month <= 9) {
18        month = "0"   month;
19    }
20    if (strDate >= 0 && strDate <= 9) {
21        strDate = "0"   strDate;
22    }
23     var currentdate = tomo.getFullYear()   seperator1   month   seperator1   strDate;
24     $(".center-day").html(currentdate);
25 }

3、获取当前本周周一和本周周日的时间范围

不管当前是周几,都可以获取到当前所在这一周的起始时间

 1   var now = new Date();//今天
 2   week(now);
 3   function week(now){
 4     var nowTime = now.getTime() ;
 5     var day = now.getDay();
 6     var oneDayLong = 24*60*60*1000 ;
 7  //获取本周所在周一
 8     var MondayTime = nowTime - (day-1)*oneDayLong  ;
 9  //获取本周所在周末
10    var SundayTime =  nowTime   (7-day)*oneDayLong ;
11  //转化日期
12   var monday = new Date(MondayTime);
13   var sunday = new Date(SundayTime);
14   var month = monday.getMonth()   1;
15   var strDate = monday.getDate();
16   var month1 = sunday.getMonth()   1;
17   var strDate1 = sunday.getDate();
18   if (month >= 1 && month <= 9) {
19       month = "0"   month;
20   }
21   if (month1 >= 1 && month1 <= 9) {
22       month1 = "0"   month1;
23   }
24   if (strDate >= 0 && strDate <= 9) {
25       strDate = "0"   strDate;
26   }
27   if (strDate1 >= 0 && strDate1 <= 9) {
28       strDate1 = "0"   strDate1;
29   }
30   currentdate = monday.getFullYear()   seperator1   month   seperator1   strDate   "至"   sunday.getFullYear()   seperator1   month1   seperator1   strDate1;
31   $(".center-day").html(currentdate);
32}   

4、点击实现每周范围的变化

点击改变的按钮将会改变显示一周范围的改变,如果有的在下一月或者下一年,那么将会自动显示,不会出现错误

 1 var now = new Date();//今天
 2 var n = 0;
 3 week(now);
 4 $("#week-add").click(function(){
 5     n  ;
 6     var date = new Date(now.getTime()   n*7*24*3600*1000);
 7     week(date);
 8 })
 9 $("#week-add").click(function(){
10     n--;
11     var date = new Date(now.getTime()   n*7*24*3600*1000);
12     week(date);
13 })
14 function week(now){
15   var nowTime = now.getTime() ;
16   var day = now.getDay();
17   var oneDayLong = 24*60*60*1000 ;
18 //获取本周所在周一
19   var MondayTime = nowTime - (day-1)*oneDayLong  ;
20 //获取本周所在周末
21   var SundayTime =  nowTime   (7-day)*oneDayLong ;
22//转化日期
23   var monday = new Date(MondayTime);
24   var sunday = new Date(SundayTime);
25   var month = monday.getMonth()   1;
26   var strDate = monday.getDate();
27   var month1 = sunday.getMonth()   1;
28   var strDate1 = sunday.getDate();
29   if (month >= 1 && month <= 9) {
30       month = "0"   month;
31   }
32   if (month1 >= 1 && month1 <= 9) {
33       month1 = "0"   month1;
34   }
35   if (strDate >= 0 && strDate <= 9) {
36      strDate = "0"   strDate;
37   }
38   if (strDate1 >= 0 && strDate1 <= 9) {
39      strDate1 = "0"   strDate1;
40   }
41   currentdate = monday.getFullYear()   seperator1   month   seperator1   strDate   "至"   sunday.getFullYear()   seperator1   month1   seperator1   strDate1;
42   $(".center-day").html(currentdate);
43 }       

5、获取当前月份的第一天和最后一天

能够获取到当前所在月份的第一天和最后一天,最后一天的日期是不固定的,能够获取到应有的日期

 1 monthfen(0)
 2 function monthfen(n){
 3   var now = new Date();//今天
 4   var firstDate = new Date((now/1000 86400*n*now.getDate())*1000);//明天
 5  //本月第一天
 6   firstDate.setDate(1); //第一天
 7   var date = new Date(firstDate);
 8   var month = date.getMonth()   1;
 9   var strDate = "0"   date.getDate();
10  //本月最后一天
11   var endDate = new Date(firstDate);
12   endDate.setMonth(firstDate.getMonth() 1);
13   endDate.setDate(0);
14   var date1 = new Date(endDate);
15   var month1 = date1.getMonth()   1;
16   var strDate1 = date1.getDate();
17   if (month >= 1 && month <= 9) {
18     month = "0"   month;
19   }
20   if (month1 >= 1 && month1 <= 9) {
21     month1 = "0"   month1;
22   }
23   currentdate = date.getFullYear()   seperator1   month   seperator1   strDate   "至"   date1.getFullYear()   seperator1   month1   seperator1   strDate1;
24   $(".center-day").html(currentdate);
25 }

6、点击实现当前月份的改变

点击按钮会实现当前月份的改变,那么最后一天的日期也会自动改变,

 1 monthfen(0)
 2 var n = 0;
 3 $("#month-add").click(function(){
 4     n  ;
 5     monthfen(n);
 6 })
 7 $("#month-less").click(function(){
 8     n--;
 9     monthfen(n);
10 })
11 function monthfen(n){
12   var now = new Date();//今天
13   var firstDate = new Date((now/1000 86400*n*now.getDate())*1000);//明天
14 //本月第一天
15   firstDate.setDate(1); //第一天
16   var date = new Date(firstDate);
17   var month = date.getMonth()   1;
18   var strDate = "0"   date.getDate();
19 //本月最后一天
20   var endDate = new Date(firstDate);
21   endDate.setMonth(firstDate.getMonth() 1);
22   endDate.setDate(0);
23   var date1 = new Date(endDate);
24   var month1 = date1.getMonth()   1;
25   var strDate1 = date1.getDate();
26   if (month >= 1 && month <= 9) {
27     month = "0"   month;
28   }
29   if (month1 >= 1 && month1 <= 9) {
30     month1 = "0"   month1;
31   }
32   currentdate = date.getFullYear()   seperator1   month   seperator1   strDate   "至"   date1.getFullYear()   seperator1   month1   seperator1   strDate1;
33   $(".center-day").html(currentdate);
34 }
当然还有很多关于日期格式的改变和算法,如果有什么不理解的可以留下评论,大家一起探讨。

js实现日期显示的一些操作相关推荐

  1. html点击按钮显示星期,js实现日期显示的一些操作(实例讲解)

    1.js获取当前日期(yyyy-mm-dd) 以下代码是获取到的当前日期: var myDate = new Date(); var year = myDate.getFullYear(); //获取 ...

  2. JS 苹果手机日期显示NaN问题

    问题描述 new Date("2019-12-29 10:30:00") 在IOS下显示为NaN 原因分析 带-的日期IOS下存在兼容问题 解决方法 字符串替换 let dateS ...

  3. html显示日期时间代码,JS全中文显示日期时间代码

    JS全中文显示日期时间代码_网页代码站(www.webdm.cn) function number(index1){ var numberstring="一二三四五六七八九十"; ...

  4. html英文日期js,JS网页上显示中英文版日期时间(根据电脑上的时间)

    JS网页上显示中英文版日期时间(根据电脑上的时间) <script language="javascript"> function shownowtime() { va ...

  5. CORE-ESP32C3|eink|日期格式化|IO11解锁|墨水屏操作库|SNTP自动同步|局部刷新|全局刷新|LuatOS-SOC接口|官方demo|学习(12):简单日期显示

    目录 基础资料 探讨重点 参考博文: 实现功能 硬件准备 软件版本 日志及soc下载工具 软件使用 接线示意图 IO11解锁教程可参考: 功能1:基于墨水屏的日期显示: 初始化: 日期显示: 功能2: ...

  6. Js获取当前日期时间及其它操作(转)

    Js获取当前日期时间及其它操作 var myDate = new Date(); myDate.getYear();        //获取当前年份(2位) myDate.getFullYear(); ...

  7. js 在一个日期上面加上指定时间(几小时、几分钟、几秒)

    在前端开发时,有时我们需要在 javascript 中对日期加上或减去指定的时间.本文简单记录如何对js日期加上或减去指定的时间. 一.使用js标准日期对象 当使用js日期对象Date进行时间加减时, ...

  8. vue 扫码页面限制区域_Vue.js 单页面多路由区域操作的实例详解

    单页面多路由区域操作 在一个页面中有两个及以上的区域,需要通过设置路由的index.js,来操作这些区域的内容 app.vue 中设置: index.js中设置: import vue from 'v ...

  9. js \n直接显示字符串_显示N个字符的最短时间

    js \n直接显示字符串 Problem statement: 问题陈述: You need to display N similar characters on a screen. You are ...

最新文章

  1. 如何定制一款12306抢票浏览器——用户界面
  2. how to make Membership header extension fields editable
  3. AngularJs入门学习
  4. 【C++】之【运算符重载函数】
  5. Python_画boxplot 盒图/箱线图
  6. 简易学生管理系统(C语言)
  7. 超炫酷的Bat脚本入门教程
  8. android 9指纹认证_Android指纹认证教程
  9. POPE-NH|1-棕榈酰基-2-油酰基磷脂酰乙醇胺POPE与NHS(N-羟基琥珀酰亚胺)酯偶联物
  10. python知道章节答案_智慧树知道Python数据分析与数据可视化答案,章节期末教程考试网课答案...
  11. 【思维导图】Excel转成思维导图
  12. hdu5879 Cure( 1/(n^2)的快速收敛性质)
  13. 盘一盘那些开设了大数据专业的中国高校
  14. matlab中imapprox,matlab图像处理命令(2)
  15. Scratch3.0中保存项目时,建议使用的扩展名是sb3
  16. 一级导数和二级导数的意义
  17. 解决报错 WARNING: IPv4 forwarding is disabled. Networking will not work.
  18. WINDOWS7-11磁盘分区教程
  19. 广播(Broadcast)的发送与接收
  20. “信息化服务包”,中小企业信息化的六件套

热门文章

  1. chrome,FireFox和Edge性能比较
  2. wifi已通过硬件开关禁用解决办法
  3. 数据库系统设计与原理
  4. nodejs crud功能(orm 七牛 mysql..)
  5. html实现汉诺塔小游戏
  6. 高德地图 判断打的点是否在圈内
  7. Python绘制万花筒
  8. 奥的斯服务器显示DBF,OTIS故障代码故障总汇01.doc
  9. Android 全屏和保持屏幕长亮
  10. Java程序设计实验3 | 面向对象(上)