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. 8   var month = date.getMonth() + 1;
  9.   var strDate = "0" + date.getDate();
  10. //本月最后一天
  11. 11   var endDate = new Date(firstDate);
  12. 12   endDate.setMonth(firstDate.getMonth()+1);
  13. 13   endDate.setDate(0);
  14.   var date1 = new Date(endDate);
  15. 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. 17   var month = date.getMonth() + 1;
  18. 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. }

  1. 当然还有很多关于日期格式的改变和算法,如果有什么不理解的可以留下评论,大家一起探讨。
  2.  

js实现日期显示的一些操作的更多相关文章

  1. Js获取日期时间及其它操作

    var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1 ...

  2. JS对日期时间的操作

    代码: //判断是否超期(有效期开始超过一年后算已超期) function IsEffect(effectDate) { var val = ""; var currentDate ...

  3. 【转】Js获取当前日期时间及其它操作

    Js获取当前日期时间及其它操作 原文地址:http://www.cnblogs.com/carekee/articles/1678041.html var myDate = new Date();my ...

  4. [转]Js获取当前日期时间及其它操作

    转载自:http://www.cnblogs.com/carekee/articles/1678041.html Js获取当前日期时间及其它操作 var myDate = new Date();myD ...

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

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

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

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

  7. iOS不得姐项目--精华模块上拉下拉的注意事项,日期显示,重构子控制器,计算cell的高度(只计算一次),图片帖子的显示

    一.上拉下拉注意事项 使用MJRefresh中的上拉控件自动设置透明 当请求下页数据通过page的时候,注意的是上拉加载更多数据失败的问题,下拉加载数据失败了,页数应该还原.或者是请求成功的时候再将页 ...

  8. JS对select动态添加option操作 (三级联动) (搜索拼接)

    以下纯属自我理解之下再东搜西查的内容~ JS对select动态添加option操作有个高大上的艺名叫多级联动:第一级改变时,第二级跟着变,第二级改变时,第三级跟着变... 本菜鸟是在工作中遇到做收货地 ...

  9. ios日期显示NaN

    ios中js通过getMonth()获取到的日期显示NaN,而在其他地方如pc.安卓都是ok的,这是为什么呢,原来这里有个ios的兼容问题,需要将日期中的“-”替换为“/” var time = ne ...

随机推荐

  1. redis五种数据类型

    string Redis的字符串和其他编程语言或者其他键值存储提供的字符串非常相似. 命令 行为 GET 获取存储在给定键中的值 SET 设置存储在给定键中的值 DEL 删除存储在给定中的值(这个命令 ...

  2. CSDN删除上传资源的办法

    转自网友:http://blog.csdn.net/ssergsw/article/details/12489101 我按照下面的方法一试,果然成功了. 昨天晚上进行测试,上传了一个压缩包和大家分享, ...

  3. 基于angularJs的单页面应用seo优化及可抓取方案原理分析

    公司使用angularJs(以下都是指ng1)框架做了互联网应用,之前没接触过seo,突然一天运营那边传来任务:要给网站做搜索引擎优化,需要研发支持.搜了下发现单页面应用做seo比较费劲,国内相关实践 ...

  4. ThinkPHP 前台视图实现类似于Yii的自动验证

    ThinkPHP model类其实自带这个功能 可以写一个基础类继承Model 模型层代码: <?php namespace Manager\Model; use Think\Model; cl ...

  5. ex3多类问题和NN中的前向传播

    ​ 昨日去了趟无锡,前天下了暴雨,所以昨天给我的感觉天气很好,天蓝云白的,以后在这边学习估计也是一件很爽的事情,且昨日通知书业寄到学校了,附赠了一份研究生数学建模的传单,我搜了搜近几年的题目,感觉统计 ...

  6. iframe 父页面与子页面之间的方法的相互调用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Ipython 自动重载

    一. 使用示例 In [1]: %load_ext autoreload In [2]: %autoreload 2 # Reload all modules (except those exclud ...

  8. docker - 启动container时出现 [warning] : ipv4 forwarding is disabled. networking will not work

    起因 今天在一台新的centos宿主机上安装docker,由于关闭了iptables,在此之后启动container的时候会出现警告: WARNING: IPv4 forwarding is disa ...

  9. Chapter 1. Introduce

    前言 本书全名是<H.264 and MPEG-4 Video Compression, Video Coding For Next-generation Multimedia>,作者为 ...

  10. 设备像素比dpr介绍

    首先介绍一下概念 devicePixelRatio其实指的是window.devicePixelRatio window.devicePixelRatio是设备上物理像素和设备独立像素(device- ...