zoukankan      html  css  js  c++  java
  • JS日期简介(二)常用日期函数

    1,日期的构造函数

    查看js构造函数内容,可以了解其所支持的参数类型

    interface DateConstructor {
    new(): Date;
    new(value: number): Date;
    new(value: string): Date;
    new(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date;
    (): string;
    readonly prototype: Date;
    /**
    * Parses a string containing a date, and returns the number of milliseconds between that date and midnight, January 1, 1970.
    * @param s A date string
    */
    parse(s: string): number;
    /**
    * Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the specified date.
    * @param year The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year.
    * @param month The month as an number between 0 and 11 (January to December).
    * @param date The date as an number between 1 and 31.
    * @param hours Must be supplied if minutes is supplied. An number from 0 to 23 (midnight to 11pm) that specifies the hour.
    * @param minutes Must be supplied if seconds is supplied. An number from 0 to 59 that specifies the minutes.
    * @param seconds Must be supplied if milliseconds is supplied. An number from 0 to 59 that specifies the seconds.
    * @param ms An number from 0 to 999 that specifies the milliseconds.
    */
    UTC(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): number;
    now(): number;
    }
     在控制台中输入可以得到如下结果:

    特别地,传月份的时候,值与实际月份的对应关系是(0~11)


    2,日期的格式转化

    toSource() 返回该对象的源代码。
    toString() 把 Date 对象转换为字符串。
    toTimeString() 把 Date 对象的时间部分转换为字符串。
    toDateString() 把 Date 对象的日期部分转换为字符串。
    toGMTString() 请使用 toUTCString() 方法代替。
    toUTCString() 根据世界时,把 Date 对象转换为字符串。
    toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串。
    toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。
    toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。


    3, 日期的常用操作

    Date() 返回当日的日期和时间。
    getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
    getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。
    getMonth() 从 Date 对象返回月份 (0 ~ 11)。
    getFullYear() 从 Date 对象以四位数字返回年份。
    getYear() 请使用 getFullYear() 方法代替。
    getHours() 返回 Date 对象的小时 (0 ~ 23)。
    getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。
    getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。
    getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。
    getTime() 返回 1970 年 1 月 1 日至今的毫秒数。



    特别地, getDay() 返回的周是(0~6)
    getMonth() 返回的是(0~11)

    同理,下面的set函数亦是如此:

    特别地,setMonth(0)表示将日期设置为上个月的最后一天

    setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。
    setMonth() 设置 Date 对象中月份 (0 ~ 11)。
    setFullYear() 设置 Date 对象中的年份(四位数字)。
    setYear() 请使用 setFullYear() 方法代替。
    setHours() 设置 Date 对象中的小时 (0 ~ 23)。
    setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。
    setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。
    setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。
    setTime() 以毫秒设置 Date 对象。

    4,日期的简单应用
    • 格式转换
      1)

      2)
    • 比较
    • 计算
  • 相关阅读:
    cocos creator基础-(七)cc.Button使用、以及不规则按钮区域的实现
    cocos creator基础-(六)cc.Sprite使用
    cocos creator基础-(五)cc.Component使用
    cocos creator基础-cc.Node(四)节点action的使用
    cocos creator基础-cc.Node(三)坐标空间转换
    cocos creator基础-cc.Node(二)事件响应
    cocos creator基础-cc.Node(一)场景树
    JS-基础-07.代码规范
    JS-基础-06.类、类的实例、继承
    drf 核心 序列化组件Serializer
  • 原文地址:https://www.cnblogs.com/perallina/p/10453072.html
Copyright © 2011-2022 走看看