• JS中Date对象使用


    //全局函数
    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

  • 相关阅读:
    html让表格边框样式为细线
    详解OpenGL中各种绘制集合图形函数
    php利用empty函数判断mysql表单是否为空
    详解OpenGL中各种绘制集合图形函数实现代码:
    约瑟夫环C语言实现源代码
    Delphi 2005 以上版本GIF动画播放设置
    INNO SETUP注册DLL文件
    DELPHI 判断文件夹是否存在,并创建
    BusinessSkinForm汉化文件“bsconst.pas”
    DELPHI2010安装Comport4
  • 原文地址:https://www.cnblogs.com/shipeng22022/p/4613981.html
Copyright © 2020-2023  润新知