Related Operation Knowledge of Date Function in JavaScript

  • 2021-07-09 06:42:05
  • OfStack

Time object is an object that we often use, and it is inseparable from this object when doing time output, time judgment and other operations. In addition to the time objects in JavaScript, there are many time objects in VbScript, and they are very easy to use. Let's explain the date function in JavaScript according to our process.

new Date()

new Date(milliseconds)

new Date(datestring)

new Date(year, month)

new Date(year, month, day)

new Date(year, month, day, hours)

new Date(year, month, day, hours, minutes)

new Date(year, month, day, hours, minutes, seconds)

new Date(year, month, day, hours, minutes, seconds, microseconds)

Below

1. new Date (), when there are no parameters, the current time and date object is created.

2. new Date (milliseconds), when the parameter is a number, then this parameter is a timestamp, which is regarded as milliseconds, and a time and date object is created from the specified milliseconds on January 1, 1970.

3. new Date (datestring), this parameter is a string, and this string 1 can be converted using Date. parse ().

4. The following six constructors are precisely defined:

1). year is an integer. If it is 0-99, then add 1900 to this, and everything else will be returned as it is.

2). month, which is an integer and ranges from 0 to 11.

3). day, which is an integer and ranges from 1 to 31.

4). hours, which is an integer and ranges from 0 to 23.

5). minutes, which is an integer and ranges from 0 to 59.

6). seconds, which is an integer and ranges from 0 to 59.

7). microseconds is an integer in the range 0-9999.


<html>
<head>
<title> The timestamp is converted to year, month, day, time, hour and second </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head>
<body>
</body>
</html>
<script>
window.onload=function(){
var now=new Date();// Current system time  
var shijianchuo = now.getTime();// Get the current timestamp 
alert(" Timestamp: "+shijianchuo);
var nowdate = new Date(shijianchuo);// Convert a timestamp to a date object 
var nowtime=nowdate.Format("yyyy-MM-dd hh:mm:ss");// Formatting the current system time is equivalent to converting the timestamp into year, month, day, hour and second 
alert(" Current time: "+nowtime);
}

/*
 Date formatting: 
 Right Date Extension of, will  Date  Object in the specified format String
 Year (y) Can be used 1-4 Placeholder, quarter (q) Can be used 1-2 Placeholder .
 Month (M) , day (d) , hours (h) , points (m) , seconds (s) Can be used 1-2 Placeholder .
 Milliseconds (S) Can only be used 1 Placeholder ( Yes 1-3 Number of bits ) 
 Examples:  
(new Date()).Format("yyyy-MM-dd hh:mm:ss.S")
(new Date()).Format("yyyy-MM-dd hh:mm:ss.S Milliseconds   No. 1 qq Quarterly ")
*/
Date.prototype.Format = function (fmt) { 
var o = {
"M+": this.getMonth() + 1, // Month  
"d+": this.getDate(), // Day  
"h+": this.getHours(), // Hour  
"m+": this.getMinutes(), // Points  
"s+": this.getSeconds(), // Seconds  
"q+": Math.floor((this.getMonth() + 3) / 3), // Quarterly  
"S": this.getMilliseconds() // Milliseconds  
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? 
         (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
</script>


Related articles: