JS Date object and get the current system time

  • 2020-03-30 01:19:41
  • OfStack

Date object
Today we will look at the Date object, which must be very familiar with the time, because many places in the website have encountered such time effects in the past. Today we're going to remember who we Date.

Let's start with his definition:
DateObj = new Date ()
DateObj = new Date (dateVal)
DateObj = new Date(year, month, Date [, hours[, minutes[, seconds[,ms]]])

Will be options. For numeric values, dateVal represents the number of milliseconds in global standard time between the specified date and midnight on January 1, 1970. If it is a string, dateVal parses it according to the rules in the parse method. The dateVal parameter can also be derived from some ActiveX® Object returns the value of VT_DATE. Will be options. The full year, for example, is 1976 (instead of 76). Will be options. Represents a month, an integer from 0 to 11 (January to December). Will be options. Represents a date, an integer from 1 to 31. Optional. Milliseconds, integers from 0 to 999.
The Date object store represents a specific time period in milliseconds. If the value of a parameter is larger than its range or negative, the other values stored are adjusted accordingly. For example, if you specify 150 seconds, JScript redefines the number to 2 minutes and 30 seconds.

If the number is NaN, the object does not represent a specific time period. If no arguments are passed to the Date object, it is initialized to the current time (UTC). The object must be assigned a value before it can be used.

Date objects can represent a range of dates approximately equal to 285,616 years before and after January 1, 1970.

The Date object has two static methods that can be called without creating a Date object. They are parse and UTC.

There are a lot of methods for Date objects, but it's pretty simple and I'm not going to list them here, OK, See Next!

Special note:

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Var myDate = new Date(2006,11,23);
Alert (" year = "+ myDate. GetYear () +" * * * * * * month = "+ myDate. GetMonth () +" * * * * * * * * day = "+ myDate. GetDate ());

The result is year=2006 *****month=11********day=23

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

This is not unusual, but when new Date is passed in with a month of 12, the problem occurs.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Var myDate = new Date(2006,12,23);
Alert (" year = "+ myDate. GetYear () +" * * * * * * month = "+ myDate. GetMonth () +" * * * * * * * * day = "+ myDate. GetDate ());

The result is year=2007 *****month=0********day=23

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

One thing I wonder. The last check shows that the value of the parameter "month" is 0~11, and the value of "12" cannot be passed in. The solution is to pass in the value of the month minus 1, and then take the value of the outside of the plus 1. I don't know who has a better way.

Such as:

Var date = new date (2010, 0-1, 15); // where 0 is January, minus 1 becomes December
  Alert (date. GetYear () + ", "+ (date, getMonth () + 1) +", "+ date. GetDate ());

Var date = new date (2010, 12-1, 15);
  Alert (date. GetYear () + ", "+ (date, getMonth () + 1) +", "+ date. GetDate ());

The argument is a string format such as 2010/1/6

Example:
Var txtDepartureDate = '2010-1-6'
Alert (txtDepartureDate. Replace (/ - / g, '/')); // replace all the '-' with/using regex
Var date = new date (txtdeparturedate. replace(/-/g,'/'));


Related articles: