SoFunction
Updated on 2025-04-12

Detailed explanation of javascript date operation (I sorted it out)

It is a built-in object—not the properties of other objects—that allow users to perform various processes of using dates and time.

Method: divided into obtaining time method, setting time method and converting time method

Get the time method:
getDate() View the Date object and return the date
getDay() Returns the day of the week
getHours() Returns the number of hours
getMinutes() Returns the number of minutes
getMonth() returns the month value
getSeconds() Returns the number of seconds
getTime() returns the full time
getYear() returns year

Notes on the specific use of date and time functions in js:When the month is obtained, it will be -1. For example, it is December, and the number obtained is 11.

var date = new Date();
();        //Get year (2 digits)
();    //Get the full year (4 digits, 1970-)
();       //Get the month (0-11,0 represents January, so () + 1 is required when displaying the current time)
();        //Acquisition date (1-31)
();          //Get the week? (0-6,0 represents Sunday)
();         //Get time (number of milliseconds starting from 1970.1.1)
();       //Get the number of hours (0-23)
();     //Get minutes (0-59)
();     //Get the number of seconds (0-59)
();    //Get milliseconds (0-999)
();        //Get date and time

How to set the time:
setDate() Changes the date of the Date object
setHours() Change the number of hours
setMinutes() Change the number of minutes
setMonth() Change month
setSeconds() Change the number of seconds
setTime() changes the full time
setYear() Change the year

Conversion time method:

toGMTString() converts the date (a value) of the Date object into a GMT time string, returning a value similar to the following: Weds,15 June l997 14:02:02 GMT (The exact format changes depending on the operating system running on the computer)
toLocaleString() converts the date (a value) of the Date object into a string, using the specific date format configured on the computer
UTC() uses Date UTC (year, month, day, hour, minute, second), to return the date in milliseconds since 00:00:00 (where hour, minute, and second are optional) on January 1, 1970.

A few things to note:

The most important point is to consider multi-browser compatibility. It is better to obtain the date in the following format

var timestart = '2015-09-05'; 
var timeend = '2015-09-06';
var time1 = (timestart+' 00:00:00').toString();
var time2 = (timeend+' 23:59:59').toString();
timestart = new Date(((/-/g,"/"))).getTime();
timeend = new Date(((/-/g,"/"))).getTime();

My editor provides you with an advertising code that was just written and displayed in a certain period of time.

<script type="text/javascript">
var timestart = '2015-09-05'; 
var timeend = '2015-09-06';
var time1 = (timestart+' 16:00:00').toString(); 
var time2 = (timeend+' 16:00:00').toString(); 
timestart = new Date(((/-/g,"/"))).getTime(); 
timeend = new Date(((/-/g,"/"))).getTime();
var nowtime=new Date().getTime();
if (nowtime>timestart && nowtime<timeend)
{
("advertise");
}
</script>

1. Get the date and year and set the date and year time. The strange problem is that you cannot set the month (comparative weird):

<script language="javascript"> 
d = new Date(); 
alert(()); 
(25); 
alert(()); 
(2000); 
alert(()); 
</script> 

2. It is best to use the getFullYear() method when you get the year.
3. Since JS starts from 0 for months, you need to add 1 when operating on the month.

Below are a few classic and often used examples about time, I hope it will improve it for everyone. Thanks for continuing to follow this post. . .

1. Convert 2005-8-5 to 2005-08-05 format

<script language="javascript"> 
var strDate = '2005-8-5'; 
((/\b(\w)\b/g, '0$1')); 
</script> 

2. Get the number of days between

&lt;script type="text/javascript"&gt; 
&lt;!-- 
alert("The number of days between:"+(new Date('2005/8/15')-new Date('2003/9/18'))/1000/60/60/24+"sky") 
//--&gt; 
&lt;/script&gt; 

3. Get the interval time

&lt;script&gt; 
var d1=new Date("2004/09/16 20:08:00"); 
var d2=new Date("2004/09/16 10:18:03"); 
var d3=d1-d2; 
var h=(d3/3600000); 
var m=((d3-h*3600000)/60000); 
var s=(d3-h*3600000-m*60000)/1000; 
alert("difference"+h+"Hour"+m+"point"+s+"Second"); 
&lt;/script&gt; 

4. Get today's date

&lt;script language="javascript"&gt; 
d = new Date(); 
alert(()+"Year"+(()+1)+"moon"+()+"day"); 
&lt;/script&gt; 


6. Transform numeric date into Chinese characters

&lt;html&gt; 
&lt;head&gt; 
&lt;title&gt; New Document &lt;/title&gt; 
&lt;/head&gt;
&lt;body&gt; 
&lt;script language=javascript&gt; 
 = function() 
{ 
var values = new Array("zero", "one", "two", "three", "Four", "five", "six", "seven", "eight", "Nine"); 
var returnValue, temp; 
returnValue = ()+"Year"; 
temp = (()+1)+"moon"+()+"day"; 
temp = (/(\d)(\d)/g,"$10$2").replace(/1ten/g,"ten").replace(/ten0/g,"ten"); 
returnValue += temp; 
returnValue = (/\d/g, function(sts){return values[parseInt(sts)]}); 
return returnValue; 
} 
var t=new Date(); 
(()); 
&lt;/script&gt; 
&lt;/body&gt; 
&lt;/html&gt;

7. Get the date of N days before or N days after


Method 1:

&lt;script type="text/javascript"&gt; 
function showdate(n) 
{ 
var uom = new Date(new Date()-0+n*86400000); 
uom = () + "-" + (()+1) + "-" + (); 
return uom; 
} 

("Today is:"+showdate(0)); 
("Yesterday was:"+showdate(-1)); 
("Tomorrow is:"+showdate(1)); 
("10 days ago:"+showdate(-10)); 
("Five days later:"+showdate(5)); 
&lt;/script&gt; 

Method 2:

&lt;script type="text/javascript"&gt; 
function showdate(n) 
{ 
var uom = new Date(); 
(()+n); 
uom = () + "-" + (()+1) + "-" + (); 
return uom; 
} 

("Today is:"+showdate(0)); 
("Yesterday was:"+showdate(-1)); 
("Tomorrow is:"+showdate(1)); 
("10 days ago:"+showdate(-10)); 
("Five days later:"+showdate(5)); 
&lt;/script&gt; 

Method 3 (Sorry, this is done with vsscript, only for learning use, and is not recommended for use on web pages, after all, IE only):

&lt;script language="vbscript"&gt; 
function showdate(n) 
showdate=dateadd("d",date(),n) 
end function 
msgbox "Today is:"&amp;showdate(0) 
msgbox "Yesterday was:"&amp;showdate(-1) 
msgbox "Tomorrow is:"&amp;showdate(1) 
msgbox "Ten days ago:"&amp;showdate(-10) 
msgbox "Five days later:"&amp;showdate(5) 
&lt;/script&gt; 

Method 4:

&lt;script language="Javascript"&gt; 
=function(){ 
var _newDate=new Date(); 
_newDate.setMonth(_newDate.getMonth()+1); 
_newDate.setDate(0); 
$_days=_newDate.getDate(); 
delete _newDate; 
return $_days; 
} 
function showdate(n) 
{ 
var uom = new Date(); 
(()+n); 
uom = () + "-" + (()+1) + "-" + ()+"\nWeek"+('Day one, two, three, four, five, six'.charAt(()))+"\nThis month"+ ()+"sky"; 
return uom; 
} 

("Today is:"+showdate(0)); 
("Yesterday was:"+showdate(-1)); 
("Tomorrow is:"+showdate(1)); 
("10 days ago:"+showdate(-10)); 
("Five days later:"+showdate(5)); 
&lt;/script&gt;