zoukankan      html  css  js  c++  java
  • JavaScript日期(参考资料)

    构造函数
    1.new Date()
      如果没有参数,则Date的构造器会依据系统设置的当前时间来创建一个Date对象。
    2.new Date(value)
      value代表自1970年1月1日00:00:00 (世界标准时间) 起经过的毫秒数。
    3.new Date(dateString)
      dateString表示日期的字符串值。该字符串应该能被 Date.parse() 方法识别。
    4.new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);


    方法
    1.Date.now()
      返回自 1970-1-1 00:00:00 UTC (世界标准时间)至今所经过的毫秒数。
    2.Date.parse()
      解析一个表示日期的字符串,并返回从 1970-1-1 00:00:00 所经过的毫秒数。
    3.Date.UTC()
      接受和构造函数最长形式的参数相同的参数(从2到7),并返回从 1970-01-01 00:00:00 UTC 开始所经过的毫秒数。
    4.Date.prototype.getFullYear()
      根据本地时间返回指定日期对象的年份。
    5.Date.prototype.getMonth()
      根据本地时间返回指定日期对象的月份(0-11)。
    6.Date.prototype.getDate()
      根据本地时间返回指定日期对象的月份中的第几天(1-31)。
    7.Date.prototype.getHours()
      根据本地时间返回指定日期对象的小时(0-23)。
    8.Date.prototype.getMinutes()
      根据本地时间返回指定日期对象的分钟(0-59)。
    9.Date.prototype.getSeconds()
      根据本地时间返回指定日期对象的秒数(0-59)。
    10.Date.prototype.getMilliseconds()
      根据本地时间返回指定日期对象的毫秒(0-999)。
    11.Date.prototype.getDay()
      根据本地时间返回指定日期对象的星期中的第几天(0-6)。
    12.Date.prototype.getTime()
      返回从1970-1-1 00:00:00 UTC(协调世界时)到该日期经过的毫秒数。
    13.Date.prototype.getTimezoneOffset()
      返回当前时区的时区偏移。
    14.Date.prototype.getUTCDate()
      根据世界时返回特定日期对象一个月的第几天(1-31)
    15.Date.prototype.setDate()
      根据本地时间为指定的日期对象设置月份中的第几天。
    16.Date.prototype.setFullYear()
      根据本地时间为指定日期对象设置完整年份。
    17.Date.prototype.setHours()
      根据本地时间为指定日期对象设置小时数。
    18.Date.prototype.setMilliseconds()
      根据本地时间为指定日期对象设置毫秒数。
    19.Date.prototype.setMinutes()
      根据本地时间为指定日期对象设置分钟数。
    20.Date.prototype.setMonth()
      根据本地时间为指定日期对象设置月份。
    21.Date.prototype.setSeconds()
      根据本地时间为指定日期对象设置秒数。
    22.Date.prototype.setTime()
      通过指定从 1970-1-1 00:00:00 UTC 开始经过的毫秒数来设置日期。
    23.Date.prototype.setUTCDate()
      根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。

    以下为示例代码

    console.log(new Date("2018-12-12"));//Wed Dec 12 2018 08:00:00 GMT+0800 (中国标准时间)
    
        //对比本地时与世界时
        var birthday = new Date(1993, 7, 23);
        var year = birthday.getFullYear();
        var month = birthday.getMonth();
        var date = birthday.getDate();
        var hours = birthday.getHours();
        var minutes = birthday.getMinutes();
        var seconds = birthday.getSeconds();
        var milliseconds = birthday.getMilliseconds();
        var day = birthday.getDay();
    
        //获取世界时
        var uyear = birthday.getUTCFullYear();
        var umonth = birthday.getUTCMonth();
        var udate = birthday.getUTCDate();
        var uhours = birthday.getUTCHours();
        var uminutes = birthday.getUTCMinutes();
        var useconds = birthday.getUTCSeconds();
        var umilliseconds = birthday.getUTCMilliseconds();
        var uday = birthday.getUTCDay();
    
        //输出本地时和世界时
        console.log(year + "." + month + "." + date + "." + hours + "." + minutes + "." + seconds + "." + milliseconds + "." + day);//1993.7.23.0.0.0.0.1
        console.log(uyear + "." + umonth + "." + udate + "." + uhours + "." + uminutes + "." + useconds + "." + umilliseconds + "." + uday);//1993.7.22.16.0.0.0.0
    
        //设置本地时
        var localDate = new Date();
        localDate.setFullYear(1993);
        localDate.setMonth(7);
        localDate.setDate(23);
        localDate.setHours(12);
        localDate.setMinutes(12);
        localDate.setSeconds(12);
        localDate.setMilliseconds(12);
    
        //设置世界时
        var worldDate = new Date();
        worldDate.setUTCFullYear(1993);
        worldDate.setUTCMonth(7);
        worldDate.setUTCDate(23);
        worldDate.setUTCHours(12);
        worldDate.setUTCMinutes(12);
        worldDate.setUTCSeconds(12);
        worldDate.setUTCMilliseconds(12);
    
        //输出本地时和世界时
        console.log(localDate); //Mon Aug 23 1993 12:12:12 GMT+0800 (中国标准时间)
        console.log(worldDate); //Mon Aug 23 1993 20:12:12 GMT+0800 (中国标准时间)
    
        //计算两个日期相隔的天数
        var startDate = new Date(2018, 10, 10);
        var endDate = new Date(2018, 10, 12);
        var intervalMilliseconds = endDate.getTime() - startDate.getTime();
        var intervalDays = intervalMilliseconds / (1000 * 60 * 60 * 24);
        console.log(intervalDays);//number:2
    
        //计算某一日期加上相应的天数得到的新日期
        var startDateTime = startDate.getTime();//起始时间毫秒数
        var newDateTime = startDateTime + 2 * 24 * 60 * 60 * 1000;//2天时间毫秒数
        var newDate = new Date(newDateTime);//新时间毫秒数
        console.log(startDate);//Sat Nov 10 2018 00:00:00 GMT+0800 (中国标准时间)
        console.log(newDate);//Mon Nov 12 2018 00:00:00 GMT+0800 (中国标准时间)
    View Code
  • 相关阅读:
    NSArray和NSMutableArray的详解
    牛一网ecshop综合类模板(仿淘常州) for ecshop 2.7.3
    编写Qt Designer自定义控件(二)——编写自定义控件界面
    android关于The connection to adb is down, and a severe error has occured.这个问题的解决办法
    jboss eap6出现Tags_$$_javassist_26 cannot be cast to javassist.util.proxy.ProxyObject的解决办法
    (5)RARP:逆地址解析协议
    构成直角三角形
    共用体和枚举类型
    通过dbcc page来查看表中的数据
    ios开发-确定/自适应textView的高度
  • 原文地址:https://www.cnblogs.com/yuyuefly/p/9663176.html
Copyright © 2011-2022 走看看