JavaScript Date

  • Last update:  2020-12-17
  • I. Overview

    This article introduces the JavaScript date methods that are commonly used and provides several examples to demonstrate usage of this methods.

    II. Commonly Used Date Methods

    Method
    Description

    getDate()

    Return the day of the month (1 ~ 31)

    getDay()

    Return the day of the week (0 ~ 6)

    getFullYear()

    Return the year

    getMonth()

    Return the month (0 ~ 11)

    getHours()

    Return the hour, according to local time (0 ~ 23)

    getMinutes()

    Return the minutes, according to local time (0 ~ 59)

    getSeconds()

    Return the seconds, according to local time (0 ~ 59)

    getMilliseconds()

    Return the milliseconds, according to local time(0 ~ 999)

    getTime()

    Return the number of milliseconds since 1970/01/01

    setDate()

    Set the day of the month (1 ~ 31)

    setMonth()

    Set the month (0 ~ 11)

    setFullYear()

    Set the year (4 digits)

    setHours()

    Set the hour (0 ~ 23)

    setMinutes()

    Set the minutes (0 ~ 59)

    setSeconds()

    Set the seconds (0 ~ 59)

    setMilliseconds()

    Set the milliseconds (0 ~ 999)

    setTime()

    Set a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970

    toDateString()

    Convert today's date into a readable string

    toTimeString()

    Convert the time portion of a Date object to a string

    toLocaleString()

    Convert a Date object to a string, using locale conventions

    toLocaleDateString()

    Return the Date object as a string, using locale conventions

    toLocaleTimeString()

    Return the time portion of a Date object as a string, using locale conventions

    Note: The date methods must be applied on a date object.

    III. Examples

    Design the parameter pane and the style is as follows:

    1606373685151730.png


    3.1 Get the current date

    Goal: When you click the "button", the date widget displays the current date.

    Add a Click event to the button, the JavaScript code is as follows:

    var mydate = new Date(); //Create a date object, the value is the current date (including hours, minutes and seconds)
    this.options.form.getWidgetByName("date").setValue(mydate); //Assign value to date widget

    The effect is as follows:

    1606373748423272.png


    3.2 Get the date value of the previous day

    You can use method setDate() to apply day addition and subtraction operations on date value. Based on the above example, the goal is to let the date widget display the day before the current date after clicking the button.

    The code to modify the click event is as follows:

    var mydate = new Date(); //Create a date object, the value is the current date (including hours, minutes and seconds)
    mydate.setDate(mydate.getDate()-1); //Set the "day" in the date object to the current day -1
    this.options.form.getWidgetByName("date").setValue(mydate); //Assign value to date widget

    The effect is as follows:

    1606373748362265.png


    3.3 Get the date value of one month before the current date

    You can use method setMonth() to apply month addition and subtraction operations on date value. The goal is to let the date widget display the date which is month before the current date after clicking the button.

    Modify the click event as follows:

    var mydate = new Date(); //Create a date object, the value is the current date (including hours, minutes and seconds)
    mydate.setMonth(mydate.getMonth()-1); //Set the "month" in the date object to the current month -1
    this.options.form.getWidgetByName("date").setValue(mydate); //Assign value to date widget

    The effect is as follows:

    1606373748782827.png

    Note: The addition and subtraction of the date, month, year, etc., are all realized by using the corresponding get method first, and then modify the value using the set method.


    Attachment List


    Theme: Secondary Development
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy