博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js时间日期处理
阅读量:4502 次
发布时间:2019-06-08

本文共 2970 字,大约阅读时间需要 9 分钟。

一、JS中Date对象常用的方法

 

 
JScript 代码   
复制
var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-????) myDate.getMonth(); //获取当前月份(0-11,0代表1月) myDate.getDate(); //获取当前日(1-31) myDate.getDay(); //获取当前星期X(0-6,0代表星期天) myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) myDate.getHours(); //获取当前小时数(0-23) myDate.getMinutes(); //获取当前分钟数(0-59) myDate.getSeconds(); //获取当前秒数(0-59) myDate.getMilliseconds(); //获取当前毫秒数(0-999) myDate.toLocaleDateString(); //获取当前日期 var mytime=myDate.toLocaleTimeString(); //获取当前时间 myDate.toLocaleString( ); //获取日期与时间

 

二、js时间日期处理实例

 

 
JScript 代码   
复制
// 增加天 function AddDays(date,value) { date.setDate(date.getDate()+value); } // 增加月 function AddMonths(date,value) { date.setMonth(date.getMonth()+value); } // 增加年 function AddYears(date,value) { date.setFullYear(date.getFullYear()+value); } // 是否为今天 function IsToday(date) { return IsDateEquals(date,new Date()); } // 是否为当月 function IsThisMonth(date) { return IsMonthEquals(date,new Date()); } // 两个日期的年是否相等 function IsMonthEquals(date1,date2) { return date1.getMonth()==date2.getMonth()&&date1.getFullYear()==date2.getFullYear(); } // 判断日期是否相等 function IsDateEquals(date1,date2) { return date1.getDate()==date2.getDate()&&IsMonthEquals(date1,date2); } // 返回某个日期对应的月份的天数 function GetMonthDayCount(date) { switch(date.getMonth()+1) { case 1:case 3:case 5:case 7:case 8:case 10:case 12: return 31; case 4:case 6:case 9:case 11: return 30; } //二月份 date=new Date(date); var lastd=28; date.setDate(29); while(date.getMonth()==1) { lastd++; AddDays(date,1); } return lastd; } // 返回两位数的年份 function GetHarfYear(date) { var v=date.getYear(); if(v>9)return v.toString(); return "0"+v; } // 返回月份(修正为两位数) function GetFullMonth(date) { var v=date.getMonth()+1; if(v>9)return v.toString(); return "0"+v; } // 返回日 (修正为两位数) function GetFullDate(date) { var v=date.getDate(); if(v>9)return v.toString(); return "0"+v; } // 替换字符串 function Replace(str,from,to) { return str.split(from).join(to); } // 格式化日期的表示 function FormatDate(date,str) { str=Replace(str,"yyyy",date.getFullYear()); str=Replace(str,"MM",GetFullMonth(date)); str=Replace(str,"dd",GetFullDate(date)); str=Replace(str,"yy",GetHarfYear(date)); str=Replace(str,"M",date.getMonth()+1); str=Replace(str,"d",date.getDate()); return str; }

 

判断是否是闰年

 
 
JScript 代码   
复制
function isLeapYear(year) { return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0); }

 

js时间差函数

 
 
JScript 代码   
复制
//addDay(-30,1); //addDay(-30,2); //addDay(-30,3); //addDay(-30,0); function addDay(days,n) { //函数说明:days日期差,n代表如下含义。 var my_date_ago=new Date(new Date() - days * 24 * 60 * 60 * 1000 * -1);//days天的日期 switch (n) { case 1: //返回年 return(my_date_ago.getFullYear()); break; case 2: //返回月 return(my_date_ago.getMonth()+1); break; case 3: //返回日 return(my_date_ago.getDate()); break; default : //返回全部 return(my_date_ago.getFullYear() + "-" + (my_date_ago.getMonth()+1) + "-" + my_date_ago.getDate()); break; } }

 

转载于:https://www.cnblogs.com/shimily/articles/4545556.html

你可能感兴趣的文章
Palindrome Partitioning
查看>>
Microservice架构模式简介
查看>>
换种形式工作
查看>>
javascript中三种典型情况下this的含义
查看>>
Python学习笔记day2(python基础一)
查看>>
【QC】安装
查看>>
628. Maximum Product of Three Numbers
查看>>
log4j Spring aop 注解的日志管理
查看>>
Spring cloud实战 从零开始一个简单搜索网站_Hystrix断路由的实现(三)
查看>>
Android服务Service
查看>>
sqlalchemy学习(一)
查看>>
silverlight Image Source URI : 一个反斜杠引发的血案
查看>>
Windows Phone开发(35):使用Express Blend绘图 转:http://blog.csdn.net/tcjiaan/article/details/7493010...
查看>>
Windows Phone开发(33):路径之其它Geometry 转:http://blog.csdn.net/tcjiaan/article/details/7483835...
查看>>
Android入门(9)AudioRecord和AudioTrack类的使用【转】http://blog.sina.com.cn/s/blog_6309e1ed0100j1rw.html...
查看>>
mybatis整合Spring编码
查看>>
第七章 路由 68 路由-前端路由和后端路由的概念
查看>>
dpkg包管理
查看>>
前端JS利用canvas的drawImage()对图片进行压缩
查看>>
一键切换皮肤的解决思想及iframe嵌套时寻找下级iframe的方法
查看>>