js实现日期显示的一些操作
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);
- }
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 ;
- //转化日期
- 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);
- 8 var month = date.getMonth() + 1;
- var strDate = "0" + date.getDate();
- //本月最后一天
- 11 var endDate = new Date(firstDate);
- 12 endDate.setMonth(firstDate.getMonth()+1);
- 13 endDate.setDate(0);
- var date1 = new Date(endDate);
- 15 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);
- 17 var month = date.getMonth() + 1;
- 18 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实现日期显示的一些操作的更多相关文章
- Js获取日期时间及其它操作
var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1 ...
- JS对日期时间的操作
代码: //判断是否超期(有效期开始超过一年后算已超期) function IsEffect(effectDate) { var val = ""; var currentDate ...
- 【转】Js获取当前日期时间及其它操作
Js获取当前日期时间及其它操作 原文地址:http://www.cnblogs.com/carekee/articles/1678041.html var myDate = new Date();my ...
- [转]Js获取当前日期时间及其它操作
转载自:http://www.cnblogs.com/carekee/articles/1678041.html Js获取当前日期时间及其它操作 var myDate = new Date();myD ...
- Js 获取当前日期时间及其它操作(转)
Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); ...
- Js获取当前日期时间及其它操作
Js获取当前日期时间及其它操作var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份 ...
- iOS不得姐项目--精华模块上拉下拉的注意事项,日期显示,重构子控制器,计算cell的高度(只计算一次),图片帖子的显示
一.上拉下拉注意事项 使用MJRefresh中的上拉控件自动设置透明 当请求下页数据通过page的时候,注意的是上拉加载更多数据失败的问题,下拉加载数据失败了,页数应该还原.或者是请求成功的时候再将页 ...
- JS对select动态添加option操作 (三级联动) (搜索拼接)
以下纯属自我理解之下再东搜西查的内容~ JS对select动态添加option操作有个高大上的艺名叫多级联动:第一级改变时,第二级跟着变,第二级改变时,第三级跟着变... 本菜鸟是在工作中遇到做收货地 ...
- ios日期显示NaN
ios中js通过getMonth()获取到的日期显示NaN,而在其他地方如pc.安卓都是ok的,这是为什么呢,原来这里有个ios的兼容问题,需要将日期中的“-”替换为“/” var time = ne ...
随机推荐
- redis五种数据类型
string Redis的字符串和其他编程语言或者其他键值存储提供的字符串非常相似. 命令 行为 GET 获取存储在给定键中的值 SET 设置存储在给定键中的值 DEL 删除存储在给定中的值(这个命令 ...
- CSDN删除上传资源的办法
转自网友:http://blog.csdn.net/ssergsw/article/details/12489101 我按照下面的方法一试,果然成功了. 昨天晚上进行测试,上传了一个压缩包和大家分享, ...
- 基于angularJs的单页面应用seo优化及可抓取方案原理分析
公司使用angularJs(以下都是指ng1)框架做了互联网应用,之前没接触过seo,突然一天运营那边传来任务:要给网站做搜索引擎优化,需要研发支持.搜了下发现单页面应用做seo比较费劲,国内相关实践 ...
- ThinkPHP 前台视图实现类似于Yii的自动验证
ThinkPHP model类其实自带这个功能 可以写一个基础类继承Model 模型层代码: <?php namespace Manager\Model; use Think\Model; cl ...
- ex3多类问题和NN中的前向传播
昨日去了趟无锡,前天下了暴雨,所以昨天给我的感觉天气很好,天蓝云白的,以后在这边学习估计也是一件很爽的事情,且昨日通知书业寄到学校了,附赠了一份研究生数学建模的传单,我搜了搜近几年的题目,感觉统计 ...
- iframe 父页面与子页面之间的方法的相互调用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Ipython 自动重载
一. 使用示例 In [1]: %load_ext autoreload In [2]: %autoreload 2 # Reload all modules (except those exclud ...
- docker - 启动container时出现 [warning] : ipv4 forwarding is disabled. networking will not work
起因 今天在一台新的centos宿主机上安装docker,由于关闭了iptables,在此之后启动container的时候会出现警告: WARNING: IPv4 forwarding is disa ...
- Chapter 1. Introduce
前言 本书全名是<H.264 and MPEG-4 Video Compression, Video Coding For Next-generation Multimedia>,作者为 ...
- 设备像素比dpr介绍
首先介绍一下概念 devicePixelRatio其实指的是window.devicePixelRatio window.devicePixelRatio是设备上物理像素和设备独立像素(device- ...