//全局函数
Date

//Date 类的静态方法

Date.parse

Date.UTC

//Date 对象的建立方法

new Date()

new Date(毫秒数)

new Date(标准时间格式字符串)

new Date(年, 月, 日, 时, 分, 秒, 毫秒)

//Date 对象的更多方法

getFullYear (getUTCFullYear)

getMonth (getUTCMonth)

getDate (getUTCDate)

getDay (getUTCDay)

getHours (getUTCHours)

getMinutes (getUTCMinutes)

getSeconds (getUTCSeconds)

getMilliseconds (getUTCMilliseconds)

getTime

getTimezoneOffset

setFullYear (setUTCFullYear)

setMonth (setUTCMonth)

setDate (setUTCDate)

setHours (setUTCHours)

setMinutes (setUTCMinutes)

setSeconds (setUTCSeconds)

setMilliseconds (setUTCMilliseconds)

setTime

toDateString

toTimeString

toUTCString

toLocaleString

toLocaleDateString

toLocaleTimeString

toString

valueOf

--------------------------------------------------------------------------------

根据一个或多个数值建立时间对象, 及本地计时与世界标准计时的区别

--------------------------------------------------------------------------------

//先用最容易理解的方式建立一个时间对象

var d = new Date(2009, 2, 27, 12, 59, 59, 999);

alert(d); //Fri Mar 27 12:59:59 UTC+0800 2009

alert(d.toString()); //Fri Mar 27 12:59:59 UTC+0800 2009

alert(d.toUTCString()); //Fri, 27 Mar 2009 04:59:59 UTC

alert(d.toLocaleString()); //2009年3月27日 12:59:59

alert(d.toDateString()); //Fri Mar 27 2009

alert(d.toLocaleDateString()); //2009年3月27日

alert(d.toTimeString()); //12:59:59 UTC+0800

alert(d.toLocaleTimeString()); //12:59:59

/* 时间在计算机中被记为一个整数, 这是从 UTC 时间的 1970-1-1 0:0:0 到此时间的毫秒数 */

alert(d.valueOf()); //1238129999999

alert(d.getTime()); //1238129999999

/* 获取本地时间和世界标准计时的时差 */

alert(d.getTimezoneOffset()); //-480; 单位是分钟, 也就是 8 小时

/* 直接使用时间值(毫秒数, 譬如上面的: 1238129999999)建立时间对象 */

var d = new Date(1238129999999);

alert(d.toLocaleString()); //2009年3月27日 12:59:59

/* 但建立函数有 2-7 个参数时, 将是根据 "年, 月, 日, 时, 分, 秒, 毫秒" 建立时间 */

d = new Date(2009, 2, 27, 12, 59, 59, 999);

alert(d.toLocaleString()); //2009年3月27日 12:59:59

d = new Date(2009, 2, 27, 12, 59, 59);

alert(d.toLocaleString()); //2009年3月27日 12:59:59

d = new Date(2009, 2, 27, 12, 59);

alert(d.toLocaleString()); //2009年3月27日 12:59:00

d = new Date(2009, 2, 27, 12);

alert(d.toLocaleString()); //2009年3月27日 12:00:00

d = new Date(2009, 2, 27);

alert(d.toLocaleString()); //2009年3月27日 0:00:00

d = new Date(2009, 2);

alert(d.toLocaleString()); //2009年3月1日 0:00:00

/* Date 类的静态函数 UTC 的参数也是和上面一样的 2-7 个, 但 UTC 获取的是个 number */

var n = Date.UTC(2009, 0); //这只是获取了那个表示时间的毫秒数

alert(typeof n); //number

var d = new Date(n); //根据刚刚获取的数、重新建立为时间对象

alert(d.toUTCString()); //Thu, 1 Jan 2009 00:00:00 UTC

alert(d.toLocaleString()); //2009年1月1日 8:00:00

--------------------------------------------------------------------------------

无参数建立的时间对象、和用全局函数 Date 获取的时间的区别

--------------------------------------------------------------------------------

var d1 = new Date(); //返回时间对象

var d2 = Date(); //返回时间字符串

alert(d1); //Fri Feb 27 13:20:58 UTC+0800 2009

alert(d2); //Fri Feb 27 13:20:58 2009

alert(d1.valueOf()); //1235712058340

alert(d2.valueOf()); //Fri Feb 27 13:20:58 2009

alert(typeof d1); //object

alert(typeof d2); //string

//明显看出 d2 只是字符串, 它可以使用 String 类的方法, 而不能使用 Date 类的方法.

--------------------------------------------------------------------------------

使用字符串参数建立时间对象

--------------------------------------------------------------------------------

var d;

d = new Date('Fri Mar 27 12:59:59 UTC+0800 2009');

alert(d.toLocaleString()); //2009年3月27日 12:59:59

d = new Date('Fri Mar 27 12:59:59 2009');

alert(d.toLocaleString()); //2009年3月27日 12:59:59

d = new Date('Fri Mar 27 2009');

alert(d.toLocaleString()); //2009年3月27日 0:00:00

d = new Date('Mar 27 2009');

alert(d.toLocaleString()); //2009年3月27日 0:00:00

/* 可使用 Date() 返回的字符串 */

var ts = Date();

d = new Date(ts);

alert(d.toLocaleString()); //2009年3月27日 14:04:38

/* Date 类的静态函数 parse 也是需要一样的字符参数, 不过它返回的是个数字(那个毫秒数) */

var n;

n = Date.parse('Mar 27 2009');

alert(n); //1238083200000

alert(typeof n); //number

d = new Date(n);

alert(d.toLocaleString()); //2009年3月27日 0:00:00

--------------------------------------------------------------------------------

分别获取和设置: 年、月、日、时、分、秒、毫秒, 其中 "星期几" 可获取但不能设置

--------------------------------------------------------------------------------

var d = new Date(2009, 2, 27, 12, 58, 59, 999);

alert(d.toLocaleString()); //2009年3月27日 0:00:00

alert(d.getFullYear()); //2009

alert(d.getMonth()); //2 (从 0 起, 2 指 3 月)

alert(d.getDate()); //27

alert(d.getDay()); //5 (星期五)

alert(d.getHours()); //12

alert(d.getMinutes()); //58

alert(d.getSeconds()); //59

alert(d.getMilliseconds()); //999 (毫秒)

d.setFullYear(2010);

d.setMonth(1);

d.setDate(2);

d.setHours(3);

d.setMinutes(4);

d.setSeconds(5);

d.setMilliseconds(6);

alert(d.toLocaleString()); //2010年2月2日 3:04:05

alert(d.getFullYear()); //2010

alert(d.getMonth()); //1 (从 0 起, 1 指 2 月)

alert(d.getDate()); //2

alert(d.getDay()); //2 (星期二)

alert(d.getHours()); //3

alert(d.getMinutes()); //4

alert(d.getSeconds()); //5

alert(d.getMilliseconds()); //6 (毫秒)

/* 还有一个 setTime */

var d = new Date();

d.setTime(0);

alert(d.toUTCString()); //Thu, 1 Jan 1970 00:00:00 UTC

javascript中Date使用总结(转)的更多相关文章

  1. javascript中Date对象的应用——简易日历的实现

    × 目录 [1]效果 [2]HTML [3]CSS[4]JS 前面的话 简易日历作为javascript中Date对象的常见应用,用途较广泛.本文将详细说明简易日历的实现思路 效果演示 HTML说明 ...

  2. javascript中Date对象的应用

    前面的话 简易日历作为javascript中Date对象的常见应用,用途较广泛.本文将详细说明简易日历的实现思路 效果演示 HTML说明 使用type=number的两个input分别作为年和月的输入 ...

  3. Javascript中Date对象的格式化

    很多语言中都带有日期的格式化函数,而Javascript中却没有提供类似的方法.之前从某位前人的帖子中发现了下面的代码,感觉非常简洁,存留备用. /** * 时间对象的格式化; */ Date.pro ...

  4. JavaScript中date 对象常用方法

    Date 对象 Date 对象用于处理日期和时间. //创建 Date 对象的语法: var datetime = new Date();//Date 对象会自动把当前日期和时间保存为其初始值. co ...

  5. javascript中Date使用

    <script type="text/javascript">    //返回当前日期和时间        var newDate=new Date();        ...

  6. JavaScript中Date(日期对象),Math对象--学习笔记

    Date对象 1.什么是Date对象? 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 语法:var Udate=new Date();  注:初始值为当前时间(当前电脑系统 ...

  7. JavaScript中Date的一些细节

    对于开发人员来说,Date有时候或许会很重要,我们可以通过new Date()来创建一个日期对象.例如: var start = new Date(), //获取当前时间 today = new Da ...

  8. javascript中Date常用方法

    一.Date的构造函数 有四种形式的Date构造函数: //1.构造函数没有参数,则返回当前日期的Date对象 var now=new Date(); //2.构造函数的参数为日期的毫秒数,返回距离1 ...

  9. javascript中Date对象复习

    js的Date对象不怎么经常用,所以忘得差不多,复习一下 1.声明一个Date对象,默认本地当前时间 var date = new Date();//Fri Apr 28 2017 14:26:19 ...

随机推荐

  1. 【APUE】Chapter5 Standard I/O Library

    5.1 Introduction 这章介绍的standard I/O都是ISOC标准的.用这些standard I/O可以不用考虑一些buffer allocation.I/O optimal-siz ...

  2. 「日常训练」Mike and Feet(Codeforces Round #305 Div. 2 D)

    题意 (Codeforces 548D) 对一个有$n$个数的数列,我们要求其连续$x(1\le x\le n)$(对于每个$x$,这样的连续group有若干个)的最小数的最大值. 分析 这是一道用了 ...

  3. [leetcode-640-Solve the Equation]

    Solve a given equation and return the value of x in the form of string "x=#value". The equ ...

  4. Postmortem Report 第一轮迭代事后分析报告

    一.设想和目标 1.1 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件<BlueZ>是一款全新动作类塔防游戏.与市面上已经存在的塔防游戏不同 ...

  5. android-ViewList的通用ViewHold

    在写ViewList的时候要写Adapter的时候,经常大量的代码都是差不多的. 1 ViewHold 2 if(convertView ==null ){}else{} 3 setTag 4 FIn ...

  6. 关于Oracle

    Oracle初学者必知的100个问题 1. Oracle安装完成后的初始口令?  internal/oracle  sys/change_on_install  system/manager  sco ...

  7. java long值转成时间格式

    /** * 将long值转换为以小时计算的格式 * @param mss * @return */ public static String formatLongTime(long mss) { St ...

  8. DPDK如何抓包

    原创翻译,转载请注明出处. DPDK的librte_pdump库,提供了在DPDK框架下抓包的功能.这个库通过完全复制Rx和Tx的mbuf到一个新的内存池,因此它降低应用程序的性能,所以只推荐在调试的 ...

  9. Mac下安装OpenCV问题

    最近看了纹理特征方面的paper,看了一些资料之后,想要实际动手实现一下其中LBP算法,果然OpenCV中已经实现. 问题 No module named "cv2" 当我在我们项 ...

  10. JAVA课程设计——植物大战僵尸(团队)

    1.团队名称.团队成员介绍 团名:嗷嗷嗷嗷嗷 吴军霖(组长) 写得一手好代码也改得一手好bug 代码整洁好看源于强迫症 大概没有什么不会的东西叭 真正的王者段位 欧阳震霆(组员) 同样擅长写代码 在青 ...