JavaScript中的日期时间函数
1、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)
2、下面对以上几个构造函数进行简单的分析
(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() 调用该方法。
new Date(datestring) 和Date.parse(datestring) 使用的是同样的解析规律,只是一个返回 Date object 另一个返回毫秒数。官方规定它们的参数(日期字符串)需要符合 RFC2822 或者 ISO8601 的格式。举个例子,也就是对应的可以写成 "Mar 31 2015" 或者 "2015-03-31" 。
如果日期字符串不符合这两种标准,这两个函数对结果概不负责,不过就算符合标准了,结果还是有点不同的。
new Date("Mar 31 2015") // Tue Mar 31 2015 00:00:00 GMT-0400 (EDT)
new Date("2015-03-31") // Mon Mar 30 2015 20:00:00 GMT-0400 (EDT)
RFC2822 的格式如果不带时区,这两个函数会当做本地时区处理,而 ISO8601 格式的话这两个函数则会当做 UTC 时区处理。
为了防止这种情况,一种解决方案是每次格式化日期都严格指定时区,以防止各种幺蛾子情况出现,比如:
new Date("2015-03-31T00:00:00-04:00") // Tue Mar 31 2015 00:00:00 GMT-0400 (EDT)
(4)以下是其余六个构造函数中参数的精确定义
year,是一个整数,如果是0-99,那么在此基础上加1900,其他的都原样返回。
month,是一个整数,范围是0-11。
day,是一个整数,范围是1-31。
hours,是一个整数,范围是0-23。
minutes,是一个整数,范围是0-59。
seconds,是一个整数,范围是0-59。
microseconds,是一个整数,范围是0-9999。
3、Date对象的基本方法
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(); //获取当前日期 myDate.toLocaleTimeString(); //获取当前时间 myDate.toLocaleString( ); //获取日期与时间
4、日期格式化代码示例
以下代码实现了yyyy-MM-dd hh:mm:ss格式的日期字符串比较大小功能,其中的日期格式化方法可通用:
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毫秒 第q季度")
*/
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中的日期时间函数的更多相关文章
- SQL中的日期时间函数
之所以把日期时间函数单独拿出来回顾一下,是因为这一部分的内容比较独立,C#中也有类似的日期时间函数,趁着想得起来,写个标题先.
- MYSQL 中常用日期时间函数使用
MySQL Date 函数 下面的表格列出了 MySQL 中最重要的内建日期函数: 函数 描述 NOW() 返回当前的日期和时间 CURDATE() 返回当前的日期 CURTIME() 返回当前的时间 ...
- SQLite3中的日期时间函数使用小结
代码如下: import sqlite3conn = sqlite3.connect('/tmp/sqlite.db')cur = conn.cursor() 接下来干嘛呢?建一张表吧.这里需要注意的 ...
- JavaScript 中的日期和时间
前言 本篇的介绍涵盖以下部分: 1. 时间标准指的是什么?UCT和GMT 的概念.关联和区别? 2. 时间表示标准有哪些? 3. JS 中时间的处理 日期时间标准 日期的标准就不多说了 -- 公元纪年 ...
- PHP中日期时间函数date()用法总结
date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考. 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母 ...
- 【SQL】MySQL内置函数中的字符串函数和日期时间函数
字符串函数 --拼接字符串组成新的字符串 Select concat(‘A’,’B’); --返回字符串长度 Select length(“CAT”) --返回子字符串在字符串中首次出现的位置,没有返 ...
- Oracle日期时间函数大全
ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...
- ORACLE 常用函数 日期/时间函数
---------------------------------------------日期/时间函数----------------------------------------------- ...
- MySQL日期时间函数大全(转)
MySQL日期时间函数大全 DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1 ...
随机推荐
- 查看 设置mysql时区
# 查看时区 show variables like '%time_zone%'; system_time_zone CST time_zone SYSTEM # 设置全局 set global ti ...
- LR 11 代理录制步骤
1.新一个HTTP录制,到下图步骤 按下图说明设置信息,点击update 选择rl里面的代理exe(wplus_init_wsock.exe),具体路径为下图 打开浏览器设置代理(Internet选项 ...
- 《笨方法学Python》加分题32
注意一下 range 的用法.查一下 range 函数并理解它在第 22 行(我的答案),你可以直接将 elements 赋值为 range(0, 6) ,而无需使用 for 循环?在 python ...
- 使用JS在页面进行数据处理时显示等待画面
使用js在页面进行数据处理期间显示等待画面: 在页面选择执行函数进行数据处理期间,显示等待画面. <script> function fun(the,row_id) { //测试函数 // ...
- github删除
https://blog.csdn.net/weixin_42152081/article/details/80635777
- Itreatot接口实现类中modCount的作用
modCount只有在本数据结构对应迭代器中才使用,以HashMap为例: private abstract class HashIterator implements Iterator { Entr ...
- clouderamanager安装
下载地址 http://archive.cloudera.com/cm5/cm/5/ 安装 先安装manager,再安装cdh 待续
- 字典 Dictionary
字典 Dictionary {Key-Value} 1.字典是无序的,没有下标(因为有key,取值直接用key值) Key尽量不要用中文编写,以防止编码不同导致取不出来 2.字典常用方法: 查找: ① ...
- 人力资源项目中 add_account.php
add_account.php ( 文件浏览 ) <?phpinclude('db_con.php'); if(isset($_POST['save'])) { $employee_i ...
- python的文件读写笔记
读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...