JavaScript -- 时光流逝(五):js中的 Date 对象的方法

时间:2021-11-14 02:26:05

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中的 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中的数组对象排序&lpar;方法sort&lpar;&rpar;详细介绍&rpar;

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

  8. js中数组Array对象的方法sort&lpar;&rpar;的应用

    一. 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. test-output目录中找不到testng-fail&period;xml原因&plus;Reportng&plus;ant build&period;xml文件

    test-output目录中找不到testng-fail.xml原因: 在没有加入Reportng 报告的相关jar包前,在test-output目录下是有testng-fail.xml,后面加入了R ...

  2. &lbrack;流媒体&rsqb;VLC主要模块

    libvlccore vlcthread: vlc线程是libvlccore的重要组成部分,我们在src文件夹下面android.os2.posix.win32等文件夹下包含thread.c文件,说明 ...

  3. thinkphp 3&plus; 观后详解 &lpar;1&rpar;

    最近面试了一些公司,发现自己的对于架构能力的不足,于是决定开始从最基本的代码做起.先看看大牛们怎么架构整个框架的.鉴于国外的框架比较难懂,于是就选择了国内比较流行的thinkphp来进行研究. 下面写 ...

  4. sql server split函数

    --创建分割函数CREATE FUNCTION dbo.Split(@String varchar(8000),@Delimiter char(1))returns @temptable TABLE ...

  5. C&plus;&plus;中的位域(bit-filed)&colon;一种节省空间的成员

    转载自:http://www.cppblog.com/suiaiguo/archive/2009/07/16/90211.html 有一种被称为位域(bit-field) 的特殊的类数据成员,它可以被 ...

  6. xml中,button改变背景颜色方法

    在画几个设置界面,用到了button控件,对于button空间的背景色在不同状态下的颜色改变方法,做了一下尝试,发现了两种背景颜色改变的方法,就总结了下. 方法一尝试了好多遍才好,要点在于,在sele ...

  7. Top 10 JavaScript编辑器,你在用哪个?

    对于JavaScript程序员来说,目前有很多很棒的工具可供选择.文本将会讨论10个优秀的支持JavaScript,HTML5和CSS开发,并且可以使用Markdown进行文档编写的文本编辑器.为什么 ...

  8. akoj-1162-计算表达式

    计算表达式 Time Limit:1000MS  Memory Limit:65536K Total Submit:14 Accepted:7 Description 对于一个不存在括号的表达式进行计 ...

  9. GBK和UTF8的区别

    GBK的文字编码是双字节来表示的,即不论中.英文字符均使用双字节来表示,只不过为区分中文,将其最高位都定成1. UTF-8编码则是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中 ...

  10. 安装sklearn&lowbar;简练解决

    安装sklearn_简练解决 < 关键步骤标黑 > 第一步:更新pip  python -m pip install --upgrade pip 第二步:安装 scipy 在网址http: ...