Javascript time function

  • 2020-03-30 03:28:14
  • OfStack

The following is a summary of the js date function daqo, for your reference

1. Gets the current time

var date=new Date();

2. Data of known year, month and day converted to date type:


var applyDate = document.domainExceptionForm.applyDate.value;
applyDate = applyDate.split("T")[0];
var applyYear = applyDate.split("-")[0];
var applyMonth = applyDate.split("-")[1] - 1;
var applyDay = applyDate.split("-")[2];
var applyDate1 = new Date(applyYear, applyMonth, applyDay);

3. Compare whether the number of days between the two dates is greater than 5:


parseInt((date-applyDate1) / (1000 * 60 * 60 * 24)) >= 5

4. Compare the two times:


if (date.valueOf() > applyDate1.valueOf()) {
    alert(" Input date must not be less than the current date! ");
} else {
    alert("OK!");
}

5. Gets the minutes and seconds of the current time


var tody = new Date();
var nian = tody.getFullYear();
var youe = tody.getMonth() + 1;
var day = tody.getDate();
var hour = tody.getHours();
var min = tody.getMinutes();
var miao = tody.getSeconds();

6. Time addition: obtain the time after 35 days of fixed time (12 August 2006)


var d = new Date("2006 . 7 . 12");
d.setDate(d.getDate() + 35);

Date object method

The Date object gives you the Time and Date relative to the utc-universal Coordinated Time (utc-universal Coordinated Time) or the operating system on which the Flash player is running. To use the Date object method, you must first create a Date object entity (Instance).
Date objects must use Flash 5 or later player.
The methods of the Date object are not static, but when used, they can be applied to the specified individual entity.
About the methods of Date objects:       ・

getDate       |   Gets the current date based on local time ( What's the date of this month )
getDay       | Get the day of the week by local time (0-Sunday,1-Monday...)
getFullYear    | Gets the current year based on local time ( The four digits )
getHours      | Gets the current number of hours based on local time (24 hourly ,0-23)
getMilliseconds  | Gets the current number of milliseconds based on local time
getMinutes     | Gets the current number of minutes based on local time
getMonth      | Gets the current month by local time ( Note from 0 start :0-Jan,1-Feb...)
getSeconds     | Gets the current number of seconds based on local time
getTime      | To obtain UTC The format of the 1970.1.1 0:00 Number of milliseconds since
getTimezoneOffset | Gets the current time and UTC Format offset value ( In minutes )
getUTCDate     | To obtain UTC Format of the current date ( What's the date of this month )
getUTCDay     | To obtain UTC What day of the week is today (0-Sunday,1-Monday...)
getUTCFullYear   | To obtain UTC The current year of the format ( The four digits )
getUTCHours    | To obtain UTC The current number of hours of the format (24 hourly ,0-23)
getUTCMilliseconds | To obtain UTC The current number of milliseconds of the format
getUTCMinutes   | To obtain UTC The current number of minutes of the format    ・
getUTCMonth    | To obtain UTC The current month of the format ( Note from 0 start :0-Jan,1-Feb...)
getUTCSeconds   | To obtain UTC Current number of seconds of the format    ・
getYear      | Gets the current abbreviated year by local time ( Current year minus 1900)
setDate      | Set the current date ( What's the date of this month )
setFullYear    | Set the current year ( The four digits )
setHours      | Sets the current number of hours (24 hourly ,0-23)
setMilliseconds  | Sets the current number of milliseconds
setMinutes     | Set the current number of minutes
setMonth      | Set the current month ( Note from 0 start :0-Jan,1-Feb...)
setUTCMinutes   | Set up the UTC The current number of minutes of the format
setUTCMonth    | Set up the UTC The current month of the format ( Note from 0 start :0-Jan,1-Feb...)
setUTCSeconds   | Set up the UTC Current number of seconds of the format
setYear      | Sets the current abbreviated year ( Current year minus 1900)
toString      | Converts the date time value to " The date of / time " String values in the form
Date.UTC      | Returns the specified UTC Format a fixed time value for a date time
setSeconds     | Sets the current number of seconds
setTime      | Set up the UTC The format of the 1970.1.1 0:00 Number of milliseconds since
setUTCDate     | Set up the UTC Format of the current date ( What's the date of this month )
setUTCFullYear   | Set up the UTC The current year of the format ( The four digits )
setUTCHours    | Set up the UTC The current number of hours of the format (24 hourly ,0-23)
setUTCMilliseconds | Set up the UTC The current number of milliseconds of the format

8. Create a new Date object

Grammar:


new Date(); new Date(year [, month [, date [, hour [, minute [, second [, millisecond ]]]]]] );

Parameters:


year     Is a 0 to 99 The integer between, corresponding to 1900 to 1999 Year, or specify a certain year for a four-digit number;
month    Is a 0 ( January ) to 11 ( December ) Between the integer, this parameter is optional;
date     Is a 1 to 31 Between the integer, this parameter is optional;
hour     Is a 0 (0:00am) to 23 (11:00pm) Between the integer, this parameter is optional;
minute    Is a 0 to 59 Between the integer, this parameter is optional;
second    Is a 0 to 59 Between the integer, this parameter is optional;
millisecond Is a 0 to 999 Between the integer, this parameter is optional;

Here's an example of getting the current date and time:

now = new Date();

Here's an example of creating a Date object for a National Day:

national_day = new Date (49, 10, 1);

Here is an example of a new Date object that USES the getMonth, getDate, and getFullYear methods of the Date object to get the time, and then outputs it in the dynamic text box.


myDate = new Date();    
dateTextField = (mydate.getMonth() + "/" + myDate.getDate() + "/" + mydate.getFullYear());


Related articles: