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 ...
随机推荐
- 第6章 LVM详解
6.1 LVM相关概念和机制 LVM(Logical Volume Manager)可以让分区变得弹性,可以随时随地的扩大和缩小分区大小,前提是该分区是LVM格式的. lvm需要使用的软件包为lvm2 ...
- Perl模块管理
Perl模块管理 perl有自带的模块,还有第三方模块.自带的模块是随perl安装而安装好的,第三方模块需要从CPAN(Comprehensive Perl Archive Network)上下载并安 ...
- Mybatis的SqlSession运行原理
前言 SqlSession是Mybatis最重要的构建之一,可以简单的认为Mybatis一系列的配置目的是生成类似 JDBC生成的Connection对象的SqlSession对象,这样才能与数据库开 ...
- 华为路由器帧中继 FR 实验
帧中继简介 帧中继( Frame Relay)是一种用于连接计算机系统的面向分组的通信方法.它主要用在公共或专用网上的局域网互联以及广域网连接.大多数公共电信局都提供帧中继服务,把它作为建立高性能的虚 ...
- Docker在Windows上运行NetCore系列(一)使用命令控制台运行.NetCore控制台应用
系列文章:https://www.cnblogs.com/alunchen/p/10121379.html 本篇文章操作系统信息 Windows:Window 10 Visual Studio:201 ...
- 第一册:lesson thirty five。
原文: Our village . This is a photograph of our village. Our village is in a valley. It is between to ...
- [PHP] 算法-数值的整数次方的PHP实现
给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 思路: 1.指数的二进制表达10^6次方 可以表示10^110(二进制) 10^100 ...
- [android] 采用pull解析xml文件
/***********2016年5月6日 更新**********************/ 知乎:Android 中有哪几种解析 xml 的类,官方推荐哪种 ? 以及它们的原理和区别? 刘吉财: ...
- sping框架纯注解配置
1.相关注解 ①@Configuration注解-->添加了该注解在类上,就表明该类是spring的配置类.该类的作用是用来替代原来的XML配置文件的. 通过配置类创建容器时,需要使用Annot ...
- Http(s)与后台交互方式
前言 Http(s)是前后端交互的主要方式之一,交互技术主要有:Ajax(XMLHttpRequest).Fetch.地址跳转(window.open.location.href).Http(s)与后 ...