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 对象的方法的更多相关文章

  1. JavaScript -- 时光流逝(三):js中的 String 对象的方法

    JavaScript -- 知识点回顾篇(三):js中的 String 对象的方法 (1) anchor(): 创建 HTML 锚. <script type="text/javasc ...

  2. js中的 Date对象 在 IOS 手机中的兼容性问题

    项目中有个时间相关的需求,很自然的用到了 js 中的 new Date() 获取时间,浏览器使用模拟手机模式访问没有问题,但是真机测试时发现,ios系统的手机无法显示时间. 定位问题发现是 new D ...

  3. JavaScript -- 时光流逝(九):Window 对象、Navigator 对象

    JavaScript -- 知识点回顾篇(九):Window 对象.Navigator 对象 1. Window 对象 1.1 Window 对象的属性 (1) closed: 返回窗口是否已被关闭. ...

  4. JavaScript -- 时光流逝(十):Screen 对象、History 对象、Location 对象

    JavaScript -- 知识点回顾篇(十):Screen 对象.History 对象.Location 对象 1. Screen 对象 1.1 Screen 对象的属性 (1) availHeig ...

  5. JS中的Date对象

    1.构造函数 Date 对象可以通过构造函数来生成,Date 的构造函数可以放入四种不同的参数 1.1.new Date() ,返回此时的本地日期时间的date对象 let d = new Date( ...

  6. js中获取事件对象的方法小结

    原文地址:http://jingyan.baidu.com/article/d8072ac4594d6cec95cefdac.html 事件对象 的获取很简单,很久前我们就知道IE中事件对象是作为全局 ...

  7. js中的数组对象排序(方法sort()详细介绍)

    定义和用法 sort() 方法用于对数组的元素进行排序. 语法    arrayObject.sort(sortby) 参数sortby:可选.规定排序顺序.必须是函数. 返回值 对数组的引用.请注意 ...

  8. js中数组Array对象的方法sort()的应用

    一. sort()方法的介绍 //给一组数据排序 var arrNum = [12,1,9,23,56,100,88,66]; console.log("排序前的数组:"+arrN ...

  9. JS中创建自定义对象的方法

    1.直接给对象扩充属性和方法: 2.对象字面量: 3.工厂方式: 4.构造函数方式: 5.原型方式: 6.混合方式. <script> // 1.直接给对象扩充属性和方法; var cat ...

随机推荐

  1. Perl面向对象(2):对象

    本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象销毁 第3篇依赖于第2篇,第2篇依赖于1篇. 已有的代码结构 现在有父类Animal ...

  2. 五分钟彻底学会iptables防火墙--技术流ken

    iptables简介 IPTABLES 是与最新的 3.5 版本 Linux内核集成的 IP 信息包过滤系统.如果 Linux 系统连接到因特网或 LAN.服务器或连接 LAN 和因特网的代理服务器, ...

  3. Git介绍及常用操作演示(一)--技术流ken

    Git介绍 Git(读音为/gɪt/.)是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发 ...

  4. hadoop小结

    测试小结:1.如果只需要对数据集进行过滤,筛选则只需要编写Mapper类,不需要Reduce类,此时要执行下面一条语句:job.setNumReduceTesk(0);2.如果需要对处理的数据进行分组 ...

  5. [转]Illuminate Database

    本文转自:https://github.com/illuminate/database Illuminate Database The Illuminate Database component is ...

  6. MVC 视图助手书写规范及注意点

    @Html.TextBoxFor() 讲解(其他类似的 @Html.LabelFor 等)同理 @Html.TextBoxFor(model => model.SearchParams.Name ...

  7. [C#] C# 知识回顾 - Lambda

    C# 知识回顾 - Lambda 序 它是第十一个希腊字母,一个拥有失意.无奈.孤独.低调等含义的流行符号,也指示一款称为“半条命”的游戏. 不过,这次我所讲的是 C# 中的 Lambda. 目录 L ...

  8. 各种官网系统镜像文件(Windows 7 ,Windows 10,Ubuntu 18.6,Centos 6.8 ,Centos 7.6 )

    在以前的刚进去计算机行业的时候,学的第一件事就是装系统,在网上苦于找不到正版的系统,这些是一直以来,见识的比较稳定的,有些是从官网下载的系统,给大家分享一哈.大家如果有用到其他好的系统,可以给我留言或 ...

  9. MVC Post 提交表单 允许他提交参数包含html标记的解决方法

    MVC Post 提交表单的时候,如果参数中包含html标记,则需要在控制器上方加上 [ValidateInput(false)]标记后就可以正常提交表单了例如: [HttpPost] [Valida ...

  10. C#窗体越界时鼠标还能回到初始坐标位置

    对窗体加越界限制后,鼠标拖动窗体越界时,窗体不能动,鼠标位置可动,但窗体不再越界时,鼠标位置还能回到鼠标按下时相对窗体的坐标:1.首先创建一个窗体Form1,然后在窗体上拖一个button1按钮(主要 ...