zoukankan      html  css  js  c++  java
  • 日期对象

    js日期对象

    日期对象是js中的一个内置方法,用来操作日期与时间。

    语法:

    1 new Date();

    参数:

    1、new Date();

    没有参数:以本机的时间做为参考,返回一个时间对象。

    1 var sj=new Date();
    2 console.log(sj);//Thu Jul 28 2016 17:10:55 GMT+0800 (中国标准时间)

    2、new Date(年,月,日,时,分,秒);

    1 var sj=new Date(1999,1,1,1,1,1);//参数为数字
    2 console.log(sj);//Mon Feb 01 1999 01:01:01 GMT+0800 (中国标准时间)

    3、new Date('June 10,2013 12:12:12');

    1 var sj=new Date('June 10,2013 12:12:12');//参数为字符串,方式是外国人表示时间的方法
    2 console.log(sj);//Mon Jun 10 2013 12:12:12 GMT+0800 (中国标准时间)

    4、new Date().getTime();

    1 var sj=new Date().getTime();//时间戳:从1970年1月1日0时0分0秒到一个时间点之间的毫秒数
    2 console.log(sj);//1469697587820

    日期对象和本机时间没有关联,不跟随本机时间动态变化,它是根据格林尼治时间来变化的。

    获取日期对象的方法

    1 日期对象.getFullYear();     获取年
    2 日期对象.getMonth();        获取月,从0开始
    3 日期对象.getDate();         获取日 
    4 日期对象.getDay();          获取星期 (星期从周日开始,从0开始计算)
    5 日期对象.getHours();        获取小时
    6 日期对象.getMinutes();      获取分钟
    7 日期对象.getSeconds();      获取秒钟
    8 日期对象.getMilliseconds(); 获取毫秒
    9 日期对象.getTime();         获取1970-01-01 00:00:00到目前日期对象的时间差(单位:毫秒)

    注意:1s (秒)= 1000ms(毫秒)。

    设置日期的相关方法

    1 日期对象.setFullYear();      设置年
    2 日期对象.setMonth();         设置月  (月份从0开始计算)
    3 日期对象.setDate();          设置日
    4 日期对象.setHours();         设置小时
    5 日期对象.setMinutes();       设置分钟
    6 日期对象.setSeconds();       设置秒钟
    7 日期对象.setMilliseconds();  设置毫秒
    8 日期对象.setTime();          获取1970-01-01 00:00:00到目前日期对象的时间差(单位:毫秒)

    设置的时间如果是0或者是负数为自动回滚到过去的时间,如果设置的时间为超出的,就会跳到未来的时间。

  • 相关阅读:
    A visual proof that neural nets can compute any function 2
    Matrix
    Formula
    ID and CLASS
    hugeng007_diary01_the living way
    the mathematical knowledge
    sys.argv[]
    The Convolutional Networks
    DVWA之XSS (跨站脚本攻击)存储型+反射型。
    DVWA之 File Inclusion 文件包含
  • 原文地址:https://www.cnblogs.com/imguo/p/5768651.html
Copyright © 2011-2022 走看看