1. Date函数
 var now = new Date(); 获取当前日期对象
 now对象->Date.prototype->Object.prototype

将一个字符串转换为Date对象的写法:
  var str = "2012-12-12";
        var date = new Date(str);    //字符串转换为Date对象
        document.write(date.getFullYear()); //然后就可以使用Date对象的方法输出年份了
 Date.getDate()
  返回是日期对象中月份中的几号。
        var date = new Date();    //2012-12-19
        document.write(date.getDate()); //返回  19 是19号
 Date.getDay()  
  返回日期中的星期几  星期天0-星期6
        var date = new Date();
        document.write(date.getDay()); //3 星期3

Date.getFulYead()  
  返回年份  如2012。
        var date = new Date();
        document.write(date.getFullYear());  //返回2012,2012年
 Date.getHours()  
  返回日期中的小时,几点了,0-23
        var date = new Date();
        document.write(date.getHours());  //返回23,晚上11点
 Date.getMilliseconds()  
  返回日期中的毫秒数
        var date = new Date();
        document.write(date.getMilliseconds());  //返回27  当前是xx年,xx月,xx点,xx分,xx秒,xx毫秒的毫秒

Date.getMinutes()    
  返回日期中的分钟数  0-59
        var date = new Date();
        document.write(date.getMinutes());  //2012-12-19 23:22  返回22,12点22分

Date.getMonth()   
    返回日期中的月份数,返回值0(1月)-11(12月)
        var date = new Date();
        document.write(date.getMonth());  //2012-12-19  此处返回11,注意此处与通常理解有些偏差,1月份返回是0,12月返回是11

Date.getSeconds()    
  返回一个日期的描述
        var date = new Date();
        document.write(date.getSeconds());·//返回34,2012-12-19 23:27:34  27分34秒

Date.getTime()      
  将一个日期对象以毫秒形式返回
        var date = new Date();
        document.write(date.getTime());  //返回1355930928466  返回值是1970-01-01 午夜到当前时间的毫秒数。

Date.getTimezoneOffset()   
  GMT时间与本地时间差,用分钟表示
        var date = new Date();
        document.write(date.getTimezoneOffset());  //返回-480  实际上这个函数获取的是javascript运行于哪个时区。单位是分钟。

Date.getUTCDate()    
  返回Date对象中的日期值,(全球时间)
        var date = new Date();
        document.write(date.getUTCDate());  //返回19  19号

Date.getUTCDay()    
  返回Date对象中的星期几,(全球时间)
        var date = new Date();
        document.write(date.getUTCDay());  //返回3  星期3

Date.getUTCFullYear()  
  返回Date中的年份,4位,如2012,(全球时间)
        var date = new Date();
        document.write(date.getUTCFullYear());  //返回2012

Date.getUTCHours()   
  返回Date对象中的小时数,就是现在是几点,终于有一个跟getHours()不同了,应该是时差关系,返回的是全球时间里的。
        var date = new Date();
        document.write(date.getUTCHours());  //现在北京时间是2012-12-19 23:44,但是返回的是15,也就是全球时间中的小时数。

Date.getUTCMilliserconds()  
  返回Date对象中的毫秒数,(全球时间)
        var date = new Date();
        document.write(date.getMilliseconds());  //返回全球时间中的毫秒数

Date.getUTCMinutes()    
  返回Date对象中的分钟数,(全球时间)
        var date = new Date();
        document.write(date.getMinutes());  //2012-12-19 23:49  返回49,注意是全球时间,其实全球时间应该就小时不同而已吧。

Date.getUTCMonth()     
  返回Date对象中月份值,(全球时间)
        var date = new Date();
        document.write(date.getMonth());  //2012-12-19  返回11,0(1月份)-11(12月份)  

Date.getUTCSeconds()    
  返回Date对象中的秒数值
        var date = new Date();
        document.write(date.getSeconds());  //返回秒数值 返回33

Date.getYear()    
  返回Date对象中的年份值减去1900
        var date = new Date();
        document.write(date.getYear());  //2012-12-19  返回112 (2012-1900)

Date.now()    
  静态方法  //返回1970-01-01午夜到现在的时间间隔,用毫秒表述
    document.write(Date.now());  //静态方法,返回当前时间与1970-01-01的时间间隔,毫秒单位。

Date.parse()    
  解析一个日期时间字符串,返回1970-01-01午夜到给定日期之间的毫秒数
        var date = "2012-12-19";
        document.write(Date.parse(date));  //返回  1355875200000
        var da = new Date(date);
        document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate());  //输出2012-11-19  //注意月份是从0-11
 Date.setDate()  
  设置一个Date对象中的日期值,返回值用调整后的日期的毫秒表示
        var date = new Date();
        document.write(date.setDate(11));  
    var da = new Date(date);
        document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate()); //输出2012-11-11  //注意月份是从0-11,设置的时候要注意

Date.setFullYear()  
  设置一个Date对象中的年份,返回值用调整后的日期的毫秒表示。
        var date = new Date();  今天是2012-12-20
        document.write(date.setFullYear(1989)); //返回630167981030
        var da = new Date(date);
        document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate()); //输出1989-11-20

Date.setHours()  /
  设置一个Date对象中的小事数,返回值用调整后的日期的毫秒表示。
        var date = new Date();      //现在是2012-12-52 22:52
        document.write(date.setHours(5)); //返回1355954000882
        var da = new Date(date);
        document.write("<br/>" + da.getHours()); //输出05
 Date.setMilliseconds()  
  设置一个日期的毫秒数
        var date = new Date();      //现在是2012-12-20
        document.write(date.setMilliseconds(22)); //返回1356015393022    注意最后两位,无论如何刷新都是22
 Date.setMinutes()    
  设置一个日期的分钟数
        var date = new Date();      //现在是2012-12-52 22:52
        document.write(date.setMinutes(1)); //返回1356012067105
        var da = new Date(date);
        document.write("<br/>" + da.getMinutes()); //输出1
 Date.setMonth()      
  设置一个日期的月份数
        var date = new Date();      //现在是2012-12-20
        document.write(date.setMonth(2)); //返回1332255597722
        var da = new Date(date);
        document.write("<br/>" + da.getMonth()); //输出2
 Date.setSeconds()      
  设置一个日期的描述
    语法:
   date.setSeconds(seconds)
      date.setSeconds(seconds,millis)
        var date = new Date();      //现在是2012-12-20
        document.write(date.setSeconds(3)); //返回1356015783872
        var da = new Date(date);
        document.write("<br/>" + da.getSeconds()); //输出3

Date.setTime()        
  使用毫秒数设置一个时间
        var date = new Date();      //现在是2012-12-20
        document.write(date.setTime(1356015783872)); //返回1356015783872
        var da = new Date(date);
        document.write("<br/>" + da.getDate()); //输出20
 Date.setUTCDate()        
  设置一个Date对象中对应月的日期值,就是几号(全球时间)
    语法:
   date.setUTCDate(day-of-month)
        var date = new Date();      //现在是2012-12-20
        document.write(date.setUTCDate(12)); //返回1355324952003
        var da = new Date(date);
        document.write("<br/>" + da.getDate()); //输出12

Date.setUTCFullYear()     
  设置一个Date对象中对应的年份,全球时间
    语法:
   date.setUTCFullYear(year)
      date.setUTCFullYear(year,month)
     date.setUTCFullYear(year,month,day)
        var date = new Date();     
        document.write(date.setUTCFullYear(1999));
        var da = new Date(date);
        document.write("<br/>" + da.getFullYear()); //输出1999

Date.setUTCHours()      
  设置一个Date对象中对应的小时数,(全球时间)
      语法:
    date.setUTCHours(hours)
          date.setUTCHours(hours,minutes)
        date.setUTCHours(hours,minutes,seconds)
        date.setUTCHours(hours,minutes,seconds,millis)
        var date = new Date();    
        document.write(date.setUTCHours(05));
        var da = new Date(date);
        document.write("<br/>" + da.getUTCHours());

Date.setUTCMilliseconds()  
  设置一个Date对象中对应的毫秒数,(全球时间)
        var date = new Date();    
        document.write(date.setMilliseconds(05)); //注意此处无论如何刷新都是05结尾

Date.setUTCMinutes()    
  设置一个Date对象的分钟、秒钟、以及毫秒值。
    语法:
   date.setUTCMinutes(minutes)
   date.setUTCMinutes(minutes,seconds)
   date.setUTCMinutes(minutes,seconds,millis)
        var date = new Date();      //现在是2012-12-20
        document.write(date.setUTCMinutes(25)); //返回1356017146549
        var da = new Date(date);
        document.write("<br/>" + da.getUTCMinutes()); //输出5

Date.setUTCMonth()    
  设置一个Date对象的月份值及日期值
        var date = new Date();    //现在是2012-12-20
        document.write(date.setMonth(01)); //返回1329751527983
        var da = new Date(date);
        document.write("<br/>" + da.getUTCMonth()); //输出1

Date.setUTCSeconds()   
  设置一个Date的秒钟及毫秒值
        var date = new Date();     //现在是2012-12-20
        document.write(date.setUTCSeconds(01)); //返回1356017281976
        var da = new Date(date);
        document.write("<br/>" + da.getUTCSeconds()); //输出1

Date.setYears()      
  设置一个Date对象的年份值,如果给的参数在0-99之间,它将会加上1900以便把它当中1900-1999之间的年份处理。如果输入4位数,则把它当成FullYear设置
        var date = new Date();    //现在是2012-12-20
        document.write(date.setYear(22)); //返回1356017281976
        var da = new Date(date);
        document.write("<br/>" + da.getFullYear()); //输出1922

var date = new Date();    //现在是2012-12-20
        document.write(date.setYear(2011)); //返回1324395113386
        var da = new Date(date);
        document.write("<br/>" + da.getFullYear()); //输出2011

Date.toDateString()    
  以字符串的形式返回一个Date的日期部分
        var date = new Date();    
        document.write(date.toDateString("yyyy-MM-dd"));

Date.toTimeString()    
  以字符串的形式返回一个Date的时间部分
        var date = new Date();    
        document.write(date.toTimeString("yyyy-MM-dd"));

Date.toISOString()    
  将一个Date对象转换为ISO-8601格式的字符串,返回的字符串格式为yyyy-mm-ddThh:mm:ssZ
        var date = new Date();  
        document.write(date.toISOString());

Date.toJSON       
  //JSON序列化一个对象
        var date = new Date();     
        document.write(date.toJSON());

Date.toLocaleDateString()  
  以本地格式的字符串返回一个Date的日期部分,返回一个本地人可读的日期格式,日期部分
        var date = new Date();    
        document.write(date.toLocaleDateString());

Date.toLocaleString()    
  将一个Date转化难为一个本地格式的字符串
        var date = new Date();     
        document.write(date.toLocaleString());

Date.toLocaleTimeString()    
  将一个Date转化为本地的格式的时间部分
        var date = new Date();    
        document.write(date.toLocaleTimeString());

Date.toString()          
  将一个Date转换为一个字符串
        var date = new Date();   //现在是2012-12-22
        document.write(date.toString());//返回Sat Dec 22 2012 19:59:17 GMT+0800

Date.toTimeString()       
  以字符串的形式返回一个Date对象的时间部分
        var date = new Date();    
        document.write(date.toString());
 Date.toUTCString()       
  将一个Date对象转换为字符串(全球时间)
        var date = new Date();     
        document.write(date.toUTCString());

Date.UTC()           
  将一个Date对象转换毫秒的形式  静态方法
  语法:Date.UTC(year,month,day,hours,minutes,seconds,ms)
    document.write(Date.UTC(2011, 11, 11, 11, 11, 11));

Date.valueOf()         
  如果是一个Date对象,将一个Date对象转为毫秒的形式,否则不显示
        var date = "";
        document.write(date.valueOf());    //不是Date对象,不输出
        var date1 = new Date();
        document.write(date1.valueOf());   //输出1356180400916

例1:

  var mydate=new Date();  //定义日期对象

  var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];

  var mynum=mydate.getDay();//返回值存储在变量mynum中

  console.log(mydate.getDay()); //输出getDay()获取值

  console.log("今天是:"+weekday[mynum]); //输出星期几

JavaScript--Date函数的更多相关文章

  1. javascript Date 函数的坑

    Javascrip中对日期的解析.格式化简直是混乱不堪,各种的坑,攻城狮们多小心.   1. 不要用 Date("yyyy-mm-dd") 构造函数解析字符串. IE.firefo ...

  2. javascript + jquery函数大全

    JAVASCRIPT Array 函数   array创建数组 concat()连接两个或更多的数组,并返回结果. join()把数组中所有元素组成字符串. pop()删除并返回数组的最后一个元素 s ...

  3. JavaScript Date对象和函数 (一)

    JavaScript_Date对象说明  Date中文为"日期"的意思,Date继承自Object对象,此对象提供操作,显示日期与时间的函数 Date对象构造函数 Date对象具有 ...

  4. JavaScript Date对象 日期获取函数

    JavaScript Date对象使用小例子: 运行结果: 总结: 1.尽管我们认为12月是第12个月份,但是JavaScript从0开始计算月份,所以月份11表示12月: 2.nowDate.set ...

  5. Javascript常用方法函数收集(二)

    Javascript常用方法函数收集(二) 31.判断是否Touch屏幕 function isTouchScreen(){ return (('ontouchstart' in window) || ...

  6. javascript工具函数

    第一部分 JavaScript工具函数 转义特殊字符为html实体   HtmlEncode: function(str){ return str.replace(/&/g, '&') ...

  7. JavaScript 常用函数总结

    javascript函数:  ·常规函数  ·数组函数  ·日期函数  ·数学函数  ·字符串函数 .cookie函数 1.常规函数 javascript常规函数包括以下9个函数:  (1)alert ...

  8. JavaScript Date 对象

    JavaScript Date 对象 Date 对象 Date 对象用于处理日期与实际. 创建 Date 对象: new Date(). 以上四种方法同样可以创建 Date 对象: var d = n ...

  9. javascript oop深入学习笔记(二)--javascript的函数

    一.概述: 函数是进行模块化程序设计的基础, javascript重的的函数不同于其他语言,每个函数都作为一个对象被维护和运行.通过函数对象的性质,可以很方便的将一个函数赋值给一个变量或则讲函数作为参 ...

  10. JavaScript function函数种类(转)

    转自:http://www.cnblogs.com/polk6/p/3284839.html JavaScript function函数种类 本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通 ...

随机推荐

  1. Prime Path(poj 3126)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  2. Blue Jeans

    Description The Genographic Project is a research partnership between IBM and The National Geographi ...

  3. Qt Style Sheets Examples(官方例子目录,很全)

    Contents Style Sheet Usage Customizing the Foreground and Background Colors Customizing Using Dynami ...

  4. 【原生态跨平台:ASP.NET Core 1.0(非Mono)在 Ubuntu 14.04 服务器上一对一的配置实现-篇幅1】

    鸡冻人心的2016,微软高产年. build 2016后 各种干货层出不穷. 1 Win10 集成了bash  ,实现了纳德拉的成诺,Microsoft Love Linux!!! 2 跨平台  ,收 ...

  5. Code (组合数)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7184   Accepted: 3353 Description Trans ...

  6. Light OJ 1018 - Brush (IV)

    题目大意:     一个二维平面上有N个点,一把刷子,刷一次可以把一条线上的所有点都刷掉.问最少刷多少次,可以把全部的点都刷完 状态压缩DP, 用记忆化搜索来写, 需要有个优化不然会超时. ===== ...

  7. 如果在安装32位oracle 客户端组件时的情况下以64位模式运行,将出现问题

    今天要写个程序,环境是win7+ vs2008+ oracle.首先得保证能连接到数据库.确认代码是没有问题的,但是拿过来直接.报错: “尝试加载 Oracle 客户端库时引发 BadImageFor ...

  8. 高效算法——D 贪心,区间覆盖问题

    Given several segments of line (int the X axis) with coordinates [Li , Ri ]. You are to choose the m ...

  9. Directx 3D编程实例:绘制3DMesh

    最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要.写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客 ...

  10. Struts2中OGNL

    什么是OGNL OGNL:Object Graph Navigation Language  对象导航图语言 开源项目,取代页面中Java脚本,简化数据访问 和EL同属于表达式语言,但功能更为强大 让 ...