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

    学习要点:

      日期对象

      将日期对象转换为字符串

      将日期对象中的日期和时间转换为字符串

      日期对象中的日期

      日期对象中的时间

      设置日期对象中的日期

      设置日期对象中的时间

      与毫秒相关的方法

    一、日期对象

      在javascript中并没有日期型的数据类型,但是提供了一个日期对象可以操作日期和时间。

      日期对象的创建:

      new Date();

    二、将日期对象转换为字符串

      将日期对象转换为字符串可以使用以下4种方法:

      date.toString();//将日期对象转换为字符串时,采用的是本地时间

      date.toLocalString();//将日期对象转换为字符串,采用的是本地时间,显示的是地方日期的格式

      date.toUTCString();//将日期对象转换为字符串时,采用的是世界时间。

      date.toGMTString();//将日期对象转换为字符串时,采用的是GMT时间,但是已被禁止使用,一般用toUTCString()方法来替换。

    三、将日期对象中的日期和时间转换为字符串

      date.toDateString();//将日期部分转换为字符串,本地时间

      date.toLocalDateString();//将日期部分转换为字符串,采用的是本地时间,显示的是地方日期的格式

      date.toTimeString();//将时间部分转换为字符串,本地时间

      date.toLocalTimeString();将时间部分转换为字符串,采用的是本地时间,显示的是地方日期的格式

    四、日期对象中的日期

      date.getYear();//获取年份,但不建议使用。

      date.getFullYear();//获取年份,,以四位数显式,建议使用       

      date.getMonth();//获取月份,值为0-11,一月份为0,二月份为1...

      date.getDate();//获取天数,即一个月中的某一天

      date.getDay();//获取一周中的第几天,值为0-6,周日为0...

    五、日期对象中的时间

      date.getHours();//返回小时部分

      date.getMinutes();//返回分钟部分

      date.getSeconds();//返回秒钟部分    

      date.getMilliseconds();//返回毫秒部分

      date.getTime();//返回日期对象中的时间与1970年1月1日0时0分0秒所间隔的毫秒数

      date.getTimezoneoffset();//返回日期对象中的时间与UTC之间的时差数,单位为秒。

    六、设置日期对象中的日期

      date.setYear(year);//不建议使用

      date.setFullYear(year,month,day);//year四位数;month:0-11,该参数可省略;day:1-31, 该参数可省略  

      date.setMonth(month,day);//month:0-11;day:1-31, 该参数可省略 

      date.getDate(day);//day:1-31

    七、设置日期对象中的时间

      date.getHours(hours,minutes,seconds,milliseconds);//hours:0-23,minutes:0-59,可省略,seconds:0-59,可省略milliseconds:0-999,可省略

      date.getMinutes(minutes,seconds,milliseconds);//minutes:0-59,seconds:0-59,可省略milliseconds:0-999,可省略

      date.getSeconds(seconds,milliseconds);// seconds:0-59,milliseconds:0-999,可省略 

      date.getMilliseconds(milliseconds);//,milliseconds:0-999

    八、与毫秒相关的方法

      date.setTime(millisecinds);milliseconds代表设置的时间与1970年1月1日0时0分0秒所间隔的毫秒数

      date.valueOf();返回日期对象中的时间与1970年1月1日0时0分0秒所间隔的毫秒数

      date.parse(str);返回str参数所代表的时间与1970年1月1日0时0分0秒所间隔的毫秒数

      date.UTC(year,month,day,hours,minutes,seconds,milliseconds);将参数所代表的日期转换成与1970年1月1日0时0分0秒所间隔的毫秒数

     

  • 相关阅读:
    DIY 作品 及 维修 不定时更新
    置顶,博客中所有源码 github
    openwrt PandoraBox PBR-M1 极路由4 HC5962 更新固件
    使用 squid 共享 虚拟专用网至局域网
    第一次参加日语能力测试 N5
    libx264 libfdk_aac 编码 解码 详解
    开发RTSP 直播软件 H264 AAC 编码 live555 ffmpeg
    MFC Camera 摄像头预览 拍照
    http2 技术整理 nginx 搭建 http2 wireshark 抓包分析 server push 服务端推送
    plist 图集 php 批量提取 PS 一个个切
  • 原文地址:https://www.cnblogs.com/LiuJL/p/5563606.html
Copyright © 2011-2022 走看看