JavaScript -- 时光流逝(五):js中的 Date 对象的方法
JavaScript -- 知识点回顾篇(五):js中的 Date 对象的方法
Date 对象: 用于处理日期和时间。
1. Date对象的方法
<script type="text/javascript">
document.write('Date()方法:<br/>');
document.write(Date()); // 返回当日的日期和时间。
document.write('<br/><br/>'); var d1=new Date(); document.write('getDate()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getDate()); // 从 Date对象返回一个月中的某一天 (1 ~ 31)。
document.write('<br/><br/>'); document.write('getDay()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getDay()); //从 Date 对象返回一周中的某一天 (0 ~ 6)。
document.write('<br/><br/>'); document.write('getMonth()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getMonth()); // 从 Date 对象返回月份 (0 ~ 11)。
document.write('<br/><br/>'); document.write('getFullYear()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getFullYear()); // 从 Date 对象以四位数字返回年份。
document.write('<br/><br/>'); document.write('getYear()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getYear()); // 请使用 getFullYear() 方法代替。
document.write('<br/><br/>'); document.write('getHours()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getHours()); // 返回 Date 对象的小时 (0 ~ 23)。
document.write('<br/><br/>'); document.write('getMinutes()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getMinutes()); // 返回 Date 对象的分钟 (0 ~ 59)。
document.write('<br/><br/>'); document.write('getSeconds()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getSeconds()); // 返回 Date 对象的秒数 (0 ~ 59)。
document.write('<br/><br/>'); document.write('getMilliseconds()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getMilliseconds()); // 返回 Date 对象的毫秒(0 ~ 999)。
document.write('<br/><br/>'); document.write('getTime()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getTime()); // 返回 1970 年 1 月 1 日至今的毫秒数。
document.write('<br/><br/>'); document.write('getTimezoneOffset()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getTimezoneOffset()); // 返回本地时间与格林威治标准时间 (GMT) 的分钟差。
document.write('<br/><br/>'); document.write('getUTCDate()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getUTCDate()); // 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。
document.write('<br/><br/>'); document.write('getUTCDay()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getUTCDay()); // 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。
document.write('<br/><br/>'); document.write('getUTCMonth()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getUTCMonth()); // 根据世界时从 Date 对象返回月份 (0 ~ 11)。
document.write('<br/><br/>'); document.write('getUTCFullYear()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getUTCFullYear()); // 根据世界时从 Date 对象返回四位数的年份。
document.write('<br/><br/>'); document.write('getUTCHours()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getUTCHours()); // 根据世界时返回 Date 对象的小时 (0 ~ 23)。
document.write('<br/><br/>'); document.write('getUTCMinutes()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getUTCMinutes()); // 根据世界时返回 Date 对象的分钟 (0 ~ 59)。
document.write('<br/><br/>'); document.write('getUTCSeconds()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getUTCSeconds()); // 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。
document.write('<br/><br/>'); document.write('getUTCMilliseconds()方法:<br/>');
document.write(d1+'<br/>');
document.write(d1.getUTCMilliseconds()); // 根据世界时返回 Date 对象的毫秒(0 ~ 999)。
document.write('<br/><br/>'); document.write('parse()方法:<br/>');
document.write(Date.parse('Oct 28,2018')); // 返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
document.write('<br/><br/>'); var d2 = new Date();
document.write('setDate()方法:<br/>');
document.write(d2+'<br/>');
d2.setDate(11); // 设置 Date 对象中月的某一天 (1 ~ 31)。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setMonth()方法:<br/>');
document.write(d2+'<br/>');
d2.setMonth(0); // 设置 Date 对象中月份 (0 ~ 11)。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setFullYear()方法:<br/>');
document.write(d2+'<br/>');
d2.setFullYear(2020);// 设置 Date 对象中的年份(四位数字)。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setYear()方法:<br/>');
document.write(d2+'<br/>');
d2.setYear(2021);// 请使用 setFullYear() 方法代替。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setHours()方法:<br/>');
document.write(d2+'<br/>');
d2.setHours(11); // 设置 Date 对象中的小时 (0 ~ 23)。
document.write(d2)
document.write('<br/><br/>'); document.write('setMinutes()方法:<br/>');
document.write(d2+'<br/>');
d2.setMinutes(12); // 设置 Date 对象中的分钟 (0 ~ 59)。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setSeconds()方法:<br/>');
document.write(d2+'<br/>');
d2.setSeconds(13); // 设置 Date 对象中的秒钟 (0 ~ 59)。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setMilliseconds()方法:<br/>');
document.write(d2.getMilliseconds()+'<br/>');
d2.setMilliseconds(14); // 设置 Date 对象中的毫秒 (0 ~ 999)。
document.write(d2.getMilliseconds()+'<br/>');
document.write('<br/><br/>'); document.write('setTime()方法:<br/>');
document.write(d2+'<br/>');
d2.setTime(1540726004758); // 以毫秒设置 Date 对象。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setUTCDate()方法:<br/>');
document.write(d2+'<br/>');
d2.setUTCDate(15); // 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setUTCMonth()方法:<br/>');
document.write(d2+'<br/>');
d2.setUTCMonth(1); // 根据世界时设置 Date 对象中的月份 (0 ~ 11)。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setUTCFullYear()方法:<br/>');
document.write(d2+'<br/>');
d2.setUTCFullYear(2020); // 根据世界时设置 Date 对象中的年份(四位数字)。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setUTCHours()方法:<br/>');
document.write(d2+'<br/>');
d2.setUTCHours(22); // 根据世界时设置 Date 对象中的小时 (0 ~ 23)。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setUTCMinutes()方法:<br/>');
document.write(d2+'<br/>');
d2.setUTCMinutes(23); // 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setUTCSeconds()方法:<br/>');
document.write(d2+'<br/>');
d2.setUTCSeconds(24); // 根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。
document.write(d2+'<br/>');
document.write('<br/><br/>'); document.write('setUTCMilliseconds()方法:<br/>');
document.write(d2.getUTCMilliseconds()+'<br/>');
d2.setUTCMilliseconds(222); // 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
document.write(d2.getUTCMilliseconds()+'<br/>');
document.write('<br/><br/>'); document.write('toString()方法:<br/>');
document.write(d2+'<br/>');
document.write(d2.toString()); // 把 Date 对象转换为字符串。
document.write('<br/><br/>'); document.write('toTimeString()方法:<br/>');
document.write(d2+'<br/>');
document.write(d2.toTimeString()); // 把 Date 对象的时间部分转换为字符串。
document.write('<br/><br/>'); document.write('toDateString()方法:<br/>');
document.write(d2+'<br/>');
document.write(d2.toDateString()); // 把 Date 对象的日期部分转换为字符串。
document.write('<br/><br/>'); document.write('toGMTString()方法:<br/>');
document.write(d2+'<br/>');
document.write(d2.toGMTString()); // 请使用 toUTCString() 方法代替。
document.write('<br/><br/>'); document.write('toUTCString()方法:<br/>');
document.write(d2+'<br/>');
document.write(d2.toUTCString()); // 根据世界时,把 Date 对象转换为字符串。
document.write('<br/><br/>'); document.write('toLocaleString()方法:<br/>');
document.write(d2+'<br/>');
document.write(d2.toLocaleString()); // 根据本地时间格式,把 Date 对象转换为字符串。
document.write('<br/><br/>'); document.write('toLocaleTimeString()方法:<br/>');
document.write(d2+'<br/>');
document.write(d2.toLocaleTimeString()); // 根据本地时间格式,把 Date 对象的时间部分转换为字符串。
document.write('<br/><br/>'); document.write('toLocaleDateString()方法:<br/>');
document.write(d2+'<br/>');
document.write(d2.toLocaleDateString()); // 根据本地时间格式,把 Date 对象的日期部分转换为字符串。
document.write('<br/><br/>'); var d3=Date.UTC(2000,10,11);// 根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。
document.write('UTC()方法:<br/>');
document.write(d3);
document.write('<br/><br/>');
</script>
Date对象各方法的执行结果:

JavaScript -- 时光流逝(五):js中的 Date 对象的方法的更多相关文章
- JavaScript -- 时光流逝(三):js中的 String 对象的方法
JavaScript -- 知识点回顾篇(三):js中的 String 对象的方法 (1) anchor(): 创建 HTML 锚. <script type="text/javasc ...
- js中的 Date对象 在 IOS 手机中的兼容性问题
项目中有个时间相关的需求,很自然的用到了 js 中的 new Date() 获取时间,浏览器使用模拟手机模式访问没有问题,但是真机测试时发现,ios系统的手机无法显示时间. 定位问题发现是 new D ...
- JavaScript -- 时光流逝(九):Window 对象、Navigator 对象
JavaScript -- 知识点回顾篇(九):Window 对象.Navigator 对象 1. Window 对象 1.1 Window 对象的属性 (1) closed: 返回窗口是否已被关闭. ...
- JavaScript -- 时光流逝(十):Screen 对象、History 对象、Location 对象
JavaScript -- 知识点回顾篇(十):Screen 对象.History 对象.Location 对象 1. Screen 对象 1.1 Screen 对象的属性 (1) availHeig ...
- JS中的Date对象
1.构造函数 Date 对象可以通过构造函数来生成,Date 的构造函数可以放入四种不同的参数 1.1.new Date() ,返回此时的本地日期时间的date对象 let d = new Date( ...
- js中获取事件对象的方法小结
原文地址:http://jingyan.baidu.com/article/d8072ac4594d6cec95cefdac.html 事件对象 的获取很简单,很久前我们就知道IE中事件对象是作为全局 ...
- js中的数组对象排序(方法sort()详细介绍)
定义和用法 sort() 方法用于对数组的元素进行排序. 语法 arrayObject.sort(sortby) 参数sortby:可选.规定排序顺序.必须是函数. 返回值 对数组的引用.请注意 ...
- js中数组Array对象的方法sort()的应用
一. sort()方法的介绍 //给一组数据排序 var arrNum = [12,1,9,23,56,100,88,66]; console.log("排序前的数组:"+arrN ...
- JS中创建自定义对象的方法
1.直接给对象扩充属性和方法: 2.对象字面量: 3.工厂方式: 4.构造函数方式: 5.原型方式: 6.混合方式. <script> // 1.直接给对象扩充属性和方法; var cat ...
随机推荐
- JavaScript之深拷贝和浅拷贝
前言 工作中会经常遇到操作数组.对象的情况,你肯定会将原数组.对象进行‘备份’当真正对其操作时发现备份的也发生改变,此时你一脸懵逼,到时是为啥,不是已经备份了么,怎么备份的数组.对象也会发生变化.如果 ...
- Perl的die和warn函数
die和warn die可以在出现错误的时候停止程序,并给出消息.默认会输出出错的程序名称和出错行号 warn函数和die函数类似,但和die的区别是不会终止程序 die和warn的参数末尾如果给了\ ...
- MySQL系列详解一:MySQL&&多实例安装-技术流ken
简介 MySQL是一个真正的多用户.多线程SQL数据库服务器.SQL(结构化查询语言)是世界上最流行的和标准化的数据库语言,它使得存储.更新和存取信息更加容易.MySQL是一个客户机/服务器结构的实现 ...
- 部署vmware-vcsa 6.5
介绍一下vcsa vsphere的两个最重要的组件是esxi和vcenter server,esxi是虚拟化主机管理软件,而vcenter server则是管理.组织多台esxi主机的管理中心. es ...
- shell编程基础(六): 透彻解析查找命令find
find 由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下.即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只要你具有相应的权限. ...
- [android] 表格布局和绝对布局
/*****************2016年4月28日 更新*************************************/ 知乎:为什么Android没有像iOS一样提供autolay ...
- SpringBoot 2.0 报错: Failed to configure a DataSource: 'url' attribute is not specified and no embe
问题描述 *************************** APPLICATION FAILED TO START *************************** Description ...
- Java_Properties
Java_Properties类 Hashtable与HashMap区别 主要:Hashtable线程安全,同步,效率相对低下 HashMap线程不安全,异步,效率高 父类:Hashtable父类是D ...
- The JRE_HOME environment variable is not defined correctly This environment
昨天启动tomcat还好好的,今天不知道抽什么风,cmd中报错: The JRE_HOME environment variable is not defined correctly This env ...
- python进程基础
目录 进程以及状态 1. 进程 2. 进程的状态 进程的创建-multiprocessing 1. 创建进程 2. 进程pid 3. Process语法结构如下 4. 给子进程指定的函数传递参数 5. ...