Javascript中日期函数的相关操作
Date对象具有多种构造函数,下面简单列举如下:
new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year, month)
new Date(year, month, day)
new Date(year, month, day, hours)
new Date(year, month, day, hours, minutes)
new Date(year, month, day, hours, minutes, seconds)
new Date(year, month, day, hours, minutes, seconds, microseconds)
下面对以上几个构造函数进行简单的分析:
1.new Date(),没有参数的时候,创建的是当前时间日期对象。
2.new Date(milliseconds),当参数为数字的时候表示毫秒数,创建一个距离1970年1月一日指定毫秒的时间日期对象。
3.new Date(datestring),此参数是一个字符串,并且此字符串一定要能够使用Date.parse(datestring)转换。
parse() 方法可解析一个日期时间字符串,并返回 1970年1月1日午夜距离该日期时间的毫秒数。
语法:Date.parse(datestring)
参数:datestring 是必需的,表示日期和时间的字符串。
该方法是 Date 对象的静态方法。一般采用 Date.parse() 的形式来调用,而不是通过 dateobject.parse() 调用该方法。
4.以下是其余六个构造函数的精确定义:
1)year,是一个整数,如果是0-99,那么在此基础上加1900,其他的都原样返回。
2)month,是一个整数,范围是0-11。
3)day,是一个整数,范围是1-31。
4)hours,是一个整数,范围是0-23。
5)minutes,是一个整数,范围是0-59。
6)seconds,是一个整数,范围是0-59。
7)microseconds,是一个整数,范围是0-9999。
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代码实例:
window.onload=function(){ var nowtime = new Date();//获取当前系统时间对象 alert("nowtime:"+nowtime); var nowdate = nowtime.Format("yyyy-MM-dd hh:mm:ss");//格式化当前系统时间 alert("nowdate:"+nowdate); var datestring = nowtime.getTime();//获取当前系统时间的时间戳 alert("datestring:"+datestring); var mytime = new Date(datestring);//将时间戳转化为日期时间对象 alert("mytime:"+mytime); var mydate = "2016-07-07 00:00:01"; if(compareTime(mydate, nowdate)){//进行日期时间比较 alert("指定时间没过期"); }else{ alert("指定时间已过期"); } } //比较yyyy-MM-dd hh:mm:ss格式的日期时间大小 function compareTime(startDate, endDate) { var startDateTemp = startDate.split(" "); var endDateTemp = endDate.split(" "); var arrStartDate = startDateTemp[0].split("-"); var arrEndDate = endDateTemp[0].split("-"); var arrStartTime = startDateTemp[1].split(":"); var arrEndTime = endDateTemp[1].split(":"); var allStartDate = new Date(arrStartDate[0], arrStartDate[1], arrStartDate[2], arrStartTime[0], arrStartTime[1], arrStartTime[2]); var allEndDate = new Date(arrEndDate[0], arrEndDate[1], arrEndDate[2], arrEndTime[0], arrEndTime[1], arrEndTime[2]); if (allStartDate.getTime() >= allEndDate.getTime()) { return true; } else { return false; } } /* 日期格式化: 对Date的扩展,将 Date 转化为指定格式的String 年(y)可以用1-4个占位符,季度(q)可以用1-2个占位符. 月(M)、日(d)、小时(h)、分(m)、秒(s)可以用1-2个占位符. 毫秒(S)只能用1个占位符(是1-3位的数字) 例子: (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") (new Date()).Format("yyyy-MM-dd hh:mm:ss.S毫秒 第qq季度") */ Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月 "d+": this.getDate(), //日 "h+": this.getHours(), //时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S" : this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]):(("00" + o[k]).substr(("" + o[k]).length))); return fmt; }
Javascript中日期函数的相关操作的更多相关文章
- JavaScript中的DOM及相关操作
一.什么是DOM JavaScript由ECMAScript.DOM和BOM三部分组成,其中DOM代表描述网页内容的方法和接口,即文档对象模型(Document Object Model).在网页上, ...
- 深入理解JavaScript中的函数操作——《JavaScript忍者秘籍》总结
匿名函数 对于什么是匿名函数,这里就不做过多介绍了.我们需要知道的是,对于JavaScript而言,匿名函数是一个很重要且具有逻辑性的特性.通常,匿名函数的使用情况是:创建一个供以后使用的函数.简单的 ...
- JavaScript中Eval()函数的作用
这一周感觉没什么写的,不过在研究dwz源码的时候有一个eval()的方法不是很了解,分享出来一起学习 -->首先来个最简单的理解 eval可以将字符串生成语句执行,和SQL的exec()类似. ...
- 浅析 JavaScript 中的 函数 currying 柯里化
原文:浅析 JavaScript 中的 函数 currying 柯里化 何为Curry化/柯里化? curry化来源与数学家 Haskell Curry的名字 (编程语言 Haskell也是以他的名字 ...
- Javascript中的函数(三)
一:概述 函数是进行模块化程序设计的基础,编写复杂的Ajax应用程序,必须对函数有更深入的了解.JavaScript中的函数不同于其他的语言,每个函数都是作为一个对象被维护和运行的.通过函数对象的性质 ...
- Javascript中的函数数学运算
1.Math函数与属性使用语法 Math.方法名(参数1,参数2,...); Math.属性; 说明 Math函数可以没有参数,比如Math.random()函数,或有多个参数,比如Math.max( ...
- JavaScript中的函数表达式
在JavaScript中,函数是个非常重要的对象,函数通常有三种表现形式:函数声明,函数表达式和函数构造器创建的函数. 本文中主要看看函数表达式及其相关的知识点. 函数表达式 首先,看看函数表达式的表 ...
- Javascript中的函数(Function)与对象(Object)的关系
今天我们来尝试理解Function和Object.因为这个里面有些人前期可能会搞糊涂.他们之间到底是什么关系.当然也不除外当初的我. 注意:官方定义: 在Javascript中,每一个函数实际上都是一 ...
- sql数据库中日期函数---2017-04-12
一.SQLServer时间日期函数详解 1. 当前系统日期.时间 select getdate() 2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值 ...
随机推荐
- Effective STL
第9条:慎重选择删除元素的方法 删除特定值元素,vector.string.deque用erase-remove:c.erase(remove(c.begin(),c.end(),1963),c.en ...
- Apache2.2 + php-5.4.45-Win32-VC9-x86 配置
首先要注意一个问题是:网上有很多教程比如: 在Apache配置文件中添加php模块.在apache2\conf\httpd.conf中: LoadModule模块添加行: LoadModule php ...
- windows tomcat配置大全
Tomcat下JSP.Servlet和JavaBean环境的配置 第一步:下载j2sdk和tomcat:到sun官方站点()下载j2sdk,注意下载版本为Windows Offline Install ...
- Semi-prime H-numbers(筛法)
Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8069 Accepted: 3479 ...
- Ugly Numbers
Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21918 Accepted: 9788 Descrip ...
- CSUFT 1002 Robot Navigation
1002: Robot Navigation Time Limit: 1 Sec Memory Limit: 128 MB Submit: 4 Solved: 2 Descript ...
- HDU(2089),数位DP
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others ...
- #ifdef _DEBUG
#ifdef _DEBUG virtual void AssertValid() const; //assert(断言)valid(有效的,正确的) virtual void Dump(CDumpCo ...
- OS: 剪裁UIImage部分不规则区域
首先,我们需要把图片展示在界面上.很简单的操作,唯一需要注意的是由于CGContextDrawImage会使用Quartz内以左下角为(0,0)点的坐标系,所以需要使用CGContextTransla ...
- Bad apple for CSharp
前言:记得10年的时候我还在上学,有一天逛csdn看到了字符版的badapple,感觉这东西好NB啊,然后就下载了一份,最近整理博客就把他整理博客,原作者是谁真心不知道,这是在果壳看到的. Bad A ...