zoukankan      html  css  js  c++  java
  • JavaScript Date

    创建Date对象:new Date();

    Date构造器可以接受各种不同格式的日期输入表示方法,其中包括:
    年份
    月份:0(1月)到11(12月)
    日期:从1到31
    时:0到23
    分:0到59
    秒:0到59
    毫秒:0到999

    new Date()的写法

    第一种,数字的形式:new Date(2016, 5, 13, 18, 35, 8);

    第二种,字符串的形式:new Date(‘June 10, 2016 13:12:50’);

    控制台输入完整的年、月、日、时、分、秒、毫秒:new Date( 2013, 0, 24, 19, 34, 55, 956 );

    如果输入时间超出正常时间以外,Date对象会自动启动溢出式前进处理,

    1月到12月的英语单词

    一月:January
    二月:February
    三月:March
    四月:April
    五月:May
    六月:June
    七月:July
    八月:August
    九月:September
    十月:October
    十一月:November
    十二月:December

    时间换算公式

    需要把毫秒换成天、时、分、秒时,可以用下边的公式进行换算
       天:Math.floor(time / 86400)
       时:Math.floor(time % 86400 / 3600)
       分:Math.floor(time % 86400 % 3600 / 60)
       秒:Math.floor(time / 60)

    Date对象方法

    方法描述
    getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
    getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。
    getFullYear() 从 Date 对象以四位数字返回年份。
    getHours() 返回 Date 对象的小时 (0 ~ 23)。
    getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。
    getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。
    getMonth() 从 Date 对象返回月份 (0 ~ 11)。
    getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。
    getTime() 返回 1970 年 1 月 1 日至今的毫秒数。
    getTimezoneOffset() 返回本地时间与格林威治标准时间 (GMT) 的分钟差。
    getUTCDate() 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。
    getUTCDay() 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。
    getUTCFullYear() 根据世界时从 Date 对象返回四位数的年份。
    getUTCHours() 根据世界时返回 Date 对象的小时 (0 ~ 23)。
    getUTCMilliseconds() 根据世界时返回 Date 对象的毫秒(0 ~ 999)。
    getUTCMinutes() 根据世界时返回 Date 对象的分钟 (0 ~ 59)。
    getUTCMonth() 根据世界时从 Date 对象返回月份 (0 ~ 11)。
    getUTCSeconds() 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。
    getYear() 已废弃。 请使用 getFullYear() 方法代替。
    parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
    setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。
    setFullYear() 设置 Date 对象中的年份(四位数字)。
    setHours() 设置 Date 对象中的小时 (0 ~ 23)。
    setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。
    setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。
    setMonth() 设置 Date 对象中月份 (0 ~ 11)。
    setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。
    setTime() setTime() 方法以毫秒设置 Date 对象。
    setUTCDate() 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
    setUTCFullYear() 根据世界时设置 Date 对象中的年份(四位数字)。
    setUTCHours() 根据世界时设置 Date 对象中的小时 (0 ~ 23)。
    setUTCMilliseconds() 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
    setUTCMinutes() 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
    setUTCMonth() 根据世界时设置 Date 对象中的月份 (0 ~ 11)。
    setUTCSeconds() setUTCSeconds() 方法用于根据世界时 (UTC) 设置指定时间的秒字段。
    setYear() 已废弃。请使用 setFullYear() 方法代替。
    toDateString() 把 Date 对象的日期部分转换为字符串。
    toGMTString() 已废弃。请使用 toUTCString() 方法代替。
    toISOString() 使用 ISO 标准返回字符串的日期格式。
    toJSON() 以 JSON 数据格式返回日期字符串。
    toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。
    toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。
    toLocaleString() 据本地时间格式,把 Date 对象转换为字符串。
    toString() 把 Date 对象转换为字符串。
    toTimeString() 把 Date 对象的时间部分转换为字符串。
    toUTCString() 根据世界时,把 Date 对象转换为字符串。
    UTC() 根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。
    valueOf() 返回 Date 对象的原始值。

    格式化时间

     1 var newDate = new Date();
     2 Date.prototype.format = function(format) {
     3        var date = {
     4           "M+": this.getMonth() + 1,
     5           "d+": this.getDate(),
     6           "h+": this.getHours(),
     7           "m+": this.getMinutes(),
     8           "s+": this.getSeconds(),
     9           "q+": Math.floor((this.getMonth() + 3) / 3),
    10           "S+": this.getMilliseconds()
    11        };
    12        if (/(y+)/i.test(format)) {
    13               format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    14        }
    15        for (var k in date) {
    16               if (new RegExp("(" + k + ")").test(format)) {
    17                      format = format.replace(RegExp.$1, RegExp.$1.length == 1? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
    18               }
    19        }
    20        return format;
    21 }
    22 console.log(newDate.format('yyyy-MM-dd h:m:s'));

    获取时间戳

    1    console.log(new Date().valueOf())
    2    console.log(new Date()-0)
    3    console.log(Date.parse(new Date()))
  • 相关阅读:
    tar.xz文件格式的压缩与解压
    touch命令创建文件
    ls命令
    redis中键空间通知
    socket.io笔记三之子命名空间的socket连接
    Java内部类的使用小结
    如何禁用Chrome浏览器的Flash
    Hibernate Tools插件的安装和使用
    JDBC DataSource
    Tomcat中用JNDI方式加载JDBC DataSource以连接数据库
  • 原文地址:https://www.cnblogs.com/studyshufei/p/9250033.html
Copyright © 2011-2022 走看看