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 ...
随机推荐
- sharding:谁都能读懂的分库、分表、分区
本文通过大量图片来分析和描述分库.分表以及数据库分区是怎样进行的. 1.sharding前的初始数据分布 在本文中,我打算用高考考生相关信息作为实验数据.请无视表的字段是否符合现实,也请无视表的设计是 ...
- Perl构建和打包自己的模块
当写好一个或多个模块后,可以将它构建.打包成"tar.gz",以便让别人安装或者上传到CPAN(如果愿意的话).对于模块的使用者来说,也不用再使用use lib 'LIB_PATH ...
- mac os下vscode快捷键
全局 Command + Shift + P / F1 显示命令面板 Command + P 快速打开 Command + Shift + N 打开新窗口 Command + W 关闭窗口 基本 Co ...
- SpringBoot系列——WebSocket
关于websocket的介绍与实现,我之前写过一篇博客,记录了用springboot-websocket实现了私聊.群聊的简单实例,这里就只提供一个入口,不再重复的写了,WebSocket+Java ...
- SpringBoot学习(二)-->Spring的Java配置方式
二.Spring的Java配置方式 Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @Bean Spring的Java配置方式是通过 @ ...
- MVC学习之路【小补充】
1]:在js中使用ViewBag 需要添加“”,否则程序报错,无法正常运行 .例如:正确格式 var ss = "@ViewBag.ts"
- camera测试之颜色还原
测试目的:camera对色彩的还原能力 测试主要设备:24色色卡,灯箱 测试环境:1.D65/CW/A光源,照度为600±100lux,整个chart表面的亮度值相差小于10% 2.D65光源,照度为 ...
- C#常量和字段以及各种方法的语法总结
目录 一. 常量和字段.... 1 1. 常量.... 1 2.字段.... 1 二.方法.... 2 1.实例构造器和类(引用类型).... 2 2.实例构造器和结构(值类型).... 2 3.类型 ...
- 动态编程(Dynamic Programming)
本文素材来自视频,请自备梯子观看:What Is Dynamic Programming and How To Use It Dynamic Programming:动态编程分为如下几步: 将复杂问题 ...
- 蓝桥杯试题----- 打印大X
打印大X 小明希望用星号拼凑,打印出一个大X,他要求能够控制笔画的宽度和整个字的高度.为了便于比对空格,所有的空白位置都以句点符来代替. 要求输入两个整数m n,表示笔的宽度,X的高度.用空格分开(0 ...